Skip to content

Commit

Permalink
cache emscripten version
Browse files Browse the repository at this point in the history
  • Loading branch information
toyobayashi committed Jul 1, 2021
1 parent 74edc63 commit 64a7fae
Show file tree
Hide file tree
Showing 6 changed files with 47 additions and 11 deletions.
16 changes: 15 additions & 1 deletion lib/emnapi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,21 @@ function emnapi_create_external_uint8array (
function emnapi_get_emscripten_version (env: napi_env, result: Pointer<Pointer<emnapi_emscripten_version>>): emnapi.napi_status {
return emnapi.checkEnv(env, () => {
return emnapi.checkArgs(env, [result], () => {
HEAPU32[result >> 2] = emnapi.emscriptenVersionPtr
if (!emnapi.emscriptenVersionPtr) {
if (emnapi.emscriptenVersion) {
emnapi.emscriptenVersionPtr = emnapi.call_malloc(12)
const addr32 = emnapi.emscriptenVersionPtr >> 2
HEAPU32[addr32] = emnapi.emscriptenVersion.major
HEAPU32[addr32 + 1] = emnapi.emscriptenVersion.minor
HEAPU32[addr32 + 2] = emnapi.emscriptenVersion.patch
HEAPU32[result >> 2] = emnapi.emscriptenVersionPtr
} else {
HEAPU32[result >> 2] = emnapi.NULL
return emnapi.napi_set_last_error(env, emnapi.napi_status.napi_generic_failure)
}
} else {
HEAPU32[result >> 2] = emnapi.emscriptenVersionPtr
}
return emnapi.napi_clear_last_error(env)
})
})
Expand Down
27 changes: 21 additions & 6 deletions lib/runtime/bootstrap.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,27 +11,27 @@ namespace emnapi {
) => napi_value) | undefined
let pKey: Pointer<const_char_p> = NULL

export let emscriptenVersionPtr: Pointer<emnapi_emscripten_version> = NULL
export let emscriptenVersionPtr: number = NULL // Pointer<emnapi_emscripten_version>
export let emscriptenVersion: emnapi_emscripten_version | null = null

function moduleRegister (): any {
if (registered) return emnapiExports
registered = true
let env: Env | undefined
if (!pKey) pKey = call_malloc(4)
if (!emscriptenVersionPtr) emscriptenVersionPtr = call_malloc(12)
try {
env = Env.create()
emnapiExports = env.callIntoModule((envObject, scope) => {
const exports = {}
const exportsHandle = scope.add(exports)
const napiValue = _napi_register_wasm_v1!(envObject.id, exportsHandle.id,
pKey, emscriptenVersionPtr, emscriptenVersionPtr + 4, emscriptenVersionPtr + 8)
pKey,
emscriptenVersionPtr,
emscriptenVersionPtr ? emscriptenVersionPtr + 4 : NULL,
emscriptenVersionPtr ? emscriptenVersionPtr + 8 : NULL)
return (!napiValue) ? undefined : envObject.handleStore.get(napiValue)!.value
})
return emnapiExports
} catch (err) {
free(pKey, 4)
pKey = NULL
registered = false
throw err
}
Expand All @@ -41,17 +41,32 @@ namespace emnapi {
_napi_register_wasm_v1 = Module._napi_register_wasm_v1
delete Module._napi_register_wasm_v1
Module.emnapiModuleRegister = moduleRegister
if (!pKey) pKey = call_malloc(4)
if (!emscriptenVersionPtr) emscriptenVersionPtr = call_malloc(12)
let exports: any
try {
exports = moduleRegister()
} catch (err) {
free(pKey, 4)
pKey = NULL
free(emscriptenVersionPtr, 12)
emscriptenVersionPtr = NULL
if (typeof Module.onEmnapiInitialized === 'function') {
Module.onEmnapiInitialized(err || new Error(String(err)))
return
} else {
throw err
}
}
if (emscriptenVersionPtr) {
emscriptenVersion = {
major: HEAPU32[emscriptenVersionPtr >> 2],
minor: HEAPU32[(emscriptenVersionPtr >> 2) + 1],
patch: HEAPU32[(emscriptenVersionPtr >> 2) + 2]
}
free(emscriptenVersionPtr, 12)
emscriptenVersionPtr = NULL
}
const key = UTF8ToString(HEAP32[pKey >> 2]) || 'emnapiExports'
free(pKey, 4)
pKey = NULL
Expand Down
1 change: 1 addition & 0 deletions lib/runtime/util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,7 @@ namespace emnapi {
export const supportBigInt = typeof BigInt !== 'undefined'

export function free (ptr: void_p, size?: size_t): void {
if (ptr === NULL) return
if (typeof size === 'number' && size > 0) {
try {
HEAPU8.set(Array(size).fill(0), ptr)
Expand Down
8 changes: 4 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -48,13 +48,13 @@
"devDependencies": {
"@tybys/cgen": "^0.7.1",
"@types/node": "^14.14.41",
"@typescript-eslint/eslint-plugin": "^4.26.0",
"@typescript-eslint/parser": "^4.26.0",
"@typescript-eslint/eslint-plugin": "^4.28.1",
"@typescript-eslint/parser": "^4.28.1",
"chalk": "^4.1.1",
"cross-env": "^7.0.3",
"eslint": "^7.25.0",
"eslint": "^7.29.0",
"eslint-config-standard-with-typescript": "^20.0.0",
"eslint-plugin-import": "^2.22.1",
"eslint-plugin-import": "^2.23.4",
"eslint-plugin-node": "^11.1.0",
"eslint-plugin-promise": "^5.1.0",
"glob": "^7.1.7",
Expand Down
5 changes: 5 additions & 0 deletions test/emnapi/binding.c
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#include <stdlib.h>
#include <stdio.h>
#include "js_native_api.h"
#include "emnapi.h"
#include "../common.h"
Expand Down Expand Up @@ -84,6 +85,10 @@ static napi_value testGetEmscriptenVersion(napi_env env, napi_callback_info info

EXTERN_C_START
napi_value Init(napi_env env, napi_value exports) {
const emnapi_emscripten_version* emscripten_version;
NAPI_CALL(env, emnapi_get_emscripten_version(env, &emscripten_version));
printf("Init: Emscripten v%u.%u.%u\n", emscripten_version->major, emscripten_version->minor, emscripten_version->patch);

napi_property_descriptor descriptors[] = {
DECLARE_NAPI_PROPERTY("getModuleObject", getModuleObject),
DECLARE_NAPI_PROPERTY("getModuleProperty", getModuleProperty),
Expand Down
1 change: 1 addition & 0 deletions test/emnapi/emnapi.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,4 +28,5 @@ module.exports = load('emnapi').then(test_typedarray => {

const [major, minor, patch] = test_typedarray.testGetEmscriptenVersion()
assert.strictEqual(`v${major}.${minor}.${patch}`, 'v2.0.24')
console.log(`test: Emscripten v${major}.${minor}.${patch}`)
})

0 comments on commit 64a7fae

Please sign in to comment.