• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1var nullptr = 0; // emscripten doesn't like to take null as uintptr_t
2
3// arr can be a normal JS array or a TypedArray
4// dest is something like SkottieKit.HEAPF32
5// ptr can be optionally provided if the memory was already allocated.
6function copy1dArray(arr, dest, ptr) {
7  if (!arr || !arr.length) {
8    return nullptr;
9  }
10  // This was created with SkottieKit.Malloc, so it's already been copied.
11  if (arr['_ck']) {
12    return arr.byteOffset;
13  }
14  if (!ptr) {
15    ptr = SkottieKit._malloc(arr.length * dest.BYTES_PER_ELEMENT);
16  }
17  // In c++ terms, the WASM heap is a uint8_t*, a long buffer/array of single
18  // byte elements. When we run _malloc, we always get an offset/pointer into
19  // that block of memory.
20  // SkottieKit exposes some different views to make it easier to work with
21  // different types. HEAPF32 for example, exposes it as a float*
22  // However, to make the ptr line up, we have to do some pointer arithmetic.
23  // Concretely, we need to convert ptr to go from an index into a 1-byte-wide
24  // buffer to an index into a 4-byte-wide buffer (in the case of HEAPF32)
25  // and thus we divide ptr by 4.
26  dest.set(arr, ptr / dest.BYTES_PER_ELEMENT);
27  return ptr;
28}
29