-
-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
214f135
commit b0b3752
Showing
11 changed files
with
304 additions
and
11 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
function emnapi_get_module_object (env: napi_env, result: Pointer<napi_value>): emnapi.napi_status { | ||
return emnapi.preamble(env, (envObject) => { | ||
return emnapi.checkArgs(env, [result], () => { | ||
HEAP32[result >> 2] = envObject.ensureHandleId(Module) | ||
return emnapi.getReturnStatus(env) | ||
}) | ||
}) | ||
} | ||
|
||
function emnapi_get_module_property (env: napi_env, utf8name: const_char_p, result: Pointer<napi_value>): emnapi.napi_status { | ||
return emnapi.preamble(env, (envObject) => { | ||
return emnapi.checkArgs(env, [utf8name, result], () => { | ||
HEAP32[result >> 2] = envObject.ensureHandleId(Module[UTF8ToString(utf8name)]) | ||
return emnapi.getReturnStatus(env) | ||
}) | ||
}) | ||
} | ||
|
||
function emnapi_create_external_uint8array ( | ||
env: napi_env, | ||
external_data: void_p, | ||
byte_length: size_t, | ||
finalize_cb: napi_finalize, | ||
finalize_hint: void_p, | ||
result: Pointer<napi_value> | ||
): emnapi.napi_status { | ||
if (!emnapi.supportFinalizer) return emnapi.napi_set_last_error(env, emnapi.napi_status.napi_generic_failure) | ||
return emnapi.preamble(env, (envObject) => { | ||
return emnapi.checkArgs(env, [result], () => { | ||
byte_length = byte_length >>> 0 | ||
if (external_data === emnapi.NULL) { | ||
byte_length = 0 | ||
} | ||
|
||
if (byte_length > 2147483647) { | ||
throw new RangeError('Cannot create a Uint8Array larger than 2147483647 bytes') | ||
} | ||
if ((external_data + byte_length) > HEAPU8.buffer.byteLength) { | ||
throw new RangeError('Memory out of range') | ||
} | ||
const u8arr = new Uint8Array(HEAPU8.buffer, external_data, byte_length) | ||
const handle = envObject.getCurrentScope().add(u8arr) | ||
if (finalize_cb !== emnapi.NULL) { | ||
const status = emnapi.wrap(emnapi.WrapType.anonymous, env, handle.id, external_data, finalize_cb, finalize_hint, emnapi.NULL) | ||
if (status === emnapi.napi_status.napi_pending_exception) { | ||
throw envObject.tryCatch.extractException() | ||
} else if (status !== emnapi.napi_status.napi_ok) { | ||
return emnapi.napi_set_last_error(env, status) | ||
} | ||
} | ||
HEAP32[result >> 2] = handle.id | ||
return emnapi.getReturnStatus(env) | ||
}) | ||
}) | ||
} | ||
|
||
emnapiImplement('emnapi_get_module_object', emnapi_get_module_object) | ||
emnapiImplement('emnapi_get_module_property', emnapi_get_module_property) | ||
emnapiImplement('emnapi_create_external_uint8array', emnapi_create_external_uint8array) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
#include <stdlib.h> | ||
#include "js_native_api.h" | ||
#include "emnapi.h" | ||
#include "../common.h" | ||
|
||
static napi_value getModuleObject(napi_env env, napi_callback_info info) { | ||
napi_value result; | ||
NAPI_CALL(env, emnapi_get_module_object(env, &result)); | ||
|
||
return result; | ||
} | ||
|
||
static napi_value getModuleProperty(napi_env env, napi_callback_info info) { | ||
size_t argc = 1; | ||
napi_value args[1]; | ||
NAPI_CALL(env, napi_get_cb_info(env, info, &argc, args, NULL, NULL)); | ||
|
||
NAPI_ASSERT(env, argc >= 1, "Wrong number of arguments"); | ||
|
||
napi_valuetype valuetype0; | ||
NAPI_CALL(env, napi_typeof(env, args[0], &valuetype0)); | ||
|
||
NAPI_ASSERT(env, valuetype0 == napi_string, | ||
"Wrong type of arguments. Expects a string as first argument."); | ||
|
||
char name[64] = { 0 }; | ||
NAPI_CALL(env, napi_get_value_string_utf8(env, args[0], name, 64, NULL)); | ||
|
||
napi_value result; | ||
NAPI_CALL(env, emnapi_get_module_property(env, name, &result)); | ||
|
||
return result; | ||
} | ||
|
||
static void FinalizeCallback(napi_env env, | ||
void* finalize_data, | ||
void* finalize_hint) | ||
{ | ||
free(finalize_data); | ||
} | ||
|
||
static napi_value External(napi_env env, napi_callback_info info) { | ||
const uint8_t nElem = 3; | ||
int8_t* externalData = malloc(nElem*sizeof(int8_t)); | ||
externalData[0] = 0; | ||
externalData[1] = 1; | ||
externalData[2] = 2; | ||
|
||
napi_value output_array; | ||
NAPI_CALL(env, emnapi_create_external_uint8array( | ||
env, | ||
externalData, | ||
nElem*sizeof(int8_t), | ||
FinalizeCallback, | ||
NULL, // finalize_hint | ||
&output_array)); | ||
|
||
return output_array; | ||
} | ||
|
||
|
||
static napi_value NullArrayBuffer(napi_env env, napi_callback_info info) { | ||
static void* data = NULL; | ||
napi_value output_array; | ||
NAPI_CALL(env, | ||
emnapi_create_external_uint8array(env, data, 0, NULL, NULL, &output_array)); | ||
return output_array; | ||
} | ||
|
||
EXTERN_C_START | ||
napi_value Init(napi_env env, napi_value exports) { | ||
napi_property_descriptor descriptors[] = { | ||
DECLARE_NAPI_PROPERTY("getModuleObject", getModuleObject), | ||
DECLARE_NAPI_PROPERTY("getModuleProperty", getModuleProperty), | ||
DECLARE_NAPI_PROPERTY("External", External), | ||
DECLARE_NAPI_PROPERTY("NullArrayBuffer", NullArrayBuffer) | ||
}; | ||
|
||
NAPI_CALL(env, napi_define_properties( | ||
env, exports, sizeof(descriptors) / sizeof(*descriptors), descriptors)); | ||
|
||
return exports; | ||
} | ||
EXTERN_C_END |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
/* eslint-disable no-new-object */ | ||
/* eslint-disable no-new-wrappers */ | ||
/* eslint-disable camelcase */ | ||
'use strict' | ||
const assert = require('assert') | ||
const { load } = require('../util') | ||
|
||
module.exports = load('emnapi').then(test_typedarray => { | ||
const mod = test_typedarray.getModuleObject() | ||
|
||
const HEAPU8 = test_typedarray.getModuleProperty('HEAPU8') | ||
const mem = HEAPU8.buffer | ||
assert.ok(mem instanceof ArrayBuffer) | ||
assert.strictEqual(mod.HEAPU8, HEAPU8) | ||
|
||
const externalResult = test_typedarray.External() | ||
assert.ok(externalResult instanceof Uint8Array) | ||
assert.strictEqual(externalResult.length, 3) | ||
assert.strictEqual(externalResult[0], 0) | ||
assert.strictEqual(externalResult[1], 1) | ||
assert.strictEqual(externalResult[2], 2) | ||
|
||
const buffer = test_typedarray.NullArrayBuffer() | ||
assert.ok(buffer instanceof Uint8Array) | ||
assert.strictEqual(buffer.length, 0) | ||
assert.strictEqual(buffer.buffer, mem) | ||
assert.notStrictEqual(buffer.buffer.byteLength, 0) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters