• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright 2019 Google LLC
3  *
4  * Use of this source code is governed by a BSD-style license that can be
5  * found in the LICENSE file.
6  */
7 
8 #ifndef WasmCommon_DEFINED
9 #define WasmCommon_DEFINED
10 
11 #include <emscripten.h>
12 #include <emscripten/bind.h>
13 #include "include/core/SkColor.h"
14 
15 using namespace emscripten;
16 
17 // Self-documenting types
18 using JSArray = emscripten::val;
19 using JSObject = emscripten::val;
20 using JSString = emscripten::val;
21 using SkPathOrNull = emscripten::val;
22 using TypedArray = emscripten::val;
23 using Uint8Array = emscripten::val;
24 using Uint16Array = emscripten::val;
25 using Uint32Array = emscripten::val;
26 using Float32Array = emscripten::val;
27 
28 /**
29  *  Create a typed-array (in the JS heap) and initialize it with the provided
30  *  data (from the wasm heap). The caller is responsible for matching the type of data
31  *  with the specified arrayType.
32  *
33  *  TODO: can we specialize this on T and provide the correct string?
34  *          e.g. T==uint8_t --> "Uint8Array"
35  */
36 template <typename T>
MakeTypedArray(int count,const T src[],const char arrayType[])37 TypedArray MakeTypedArray(int count, const T src[], const char arrayType[]) {
38     emscripten::val length = emscripten::val(count);
39     emscripten::val jarray = emscripten::val::global(arrayType).new_(count);
40     jarray.call<void>("set", val(typed_memory_view(count, src)));
41     return jarray;
42 }
43 
44 #endif
45