Skip to content

Commit

Permalink
allow macros defined in source code (#90)
Browse files Browse the repository at this point in the history
  • Loading branch information
toyobayashi authored Nov 17, 2023
1 parent 56e6492 commit 1994ef9
Show file tree
Hide file tree
Showing 4 changed files with 407 additions and 268 deletions.
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@
"eslint-plugin-n": "^15.6.1",
"eslint-plugin-promise": "^6.1.1",
"fs-extra": "^10.1.0",
"ts-clone-node": "^3.0.0",
"typescript": "~5.0.4"
},
"workspaces": [
Expand Down
62 changes: 34 additions & 28 deletions packages/emnapi/src/macro.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,34 +4,40 @@
/* eslint-disable @typescript-eslint/no-unnecessary-type-assertion */
/* eslint-disable @typescript-eslint/explicit-function-return-type */

declare function $CHECK_ENV (env: napi_env): any
declare function $CHECK_ARG (env: Env, arg: void_p): any
declare function $PREAMBLE (env: napi_env, fn: (envObject: Env) => napi_status): napi_status
/** @macro */
function $CHECK_ENV (env: napi_env): any {
if (!env) return napi_status.napi_invalid_arg
}

// function $CHECK_ENV (env: napi_env): any {
// if (env == 0) return napi_status.napi_invalid_arg
// }
/** @macro */
function $RETURN_STATUS_IF_FALSE (env: Env, condition: any, status: napi_status): any {
if (!condition) return env.setLastError(status)
}

// function $CHECK_ARG (env: Env, arg: void_p): any {
// if (arg == 0) return env.setLastError(napi_status.napi_invalid_arg)
// }
/** @macro */
function $CHECK_ARG (env: Env, arg: void_p) {
$RETURN_STATUS_IF_FALSE!(env, arg, napi_status.napi_invalid_arg)
}

// function $PREAMBLE (env: napi_env, fn: (envObject: Env) => napi_status): napi_status {
// $CHECK_ENV!(env)
// const envObject = emnapiCtx.envStore.get(env)!
// if (envObject.tryCatch.hasCaught()) return envObject.setLastError(napi_status.napi_pending_exception)
// if (!envObject.canCallIntoJs()) {
// return envObject.setLastError(
// envObject.moduleApiVersion === Version.NAPI_VERSION_EXPERIMENTAL
// ? napi_status.napi_cannot_run_js
// : napi_status.napi_pending_exception
// )
// }
// envObject.clearLastError()
// try {
// return fn(envObject)
// } catch (err) {
// envObject.tryCatch.setError(err)
// return envObject.setLastError(napi_status.napi_pending_exception)
// }
// }
/** @macro */
function $PREAMBLE (env: napi_env, fn: (envObject: Env) => napi_status): napi_status {
$CHECK_ENV!(env)
const envObject = emnapiCtx.envStore.get(env)!
envObject.checkGCAccess()
$RETURN_STATUS_IF_FALSE!(
envObject, envObject.tryCatch.isEmpty(), napi_status.napi_pending_exception)
$RETURN_STATUS_IF_FALSE!(
envObject,
envObject.canCallIntoJs(),
envObject.moduleApiVersion === Version.NAPI_VERSION_EXPERIMENTAL
? napi_status.napi_cannot_run_js
: napi_status.napi_pending_exception
)
envObject.clearLastError()
try {
return fn(envObject)
} catch (err) {
envObject.tryCatch.setError(err)
return envObject.setLastError(napi_status.napi_pending_exception)
}
}
Loading

0 comments on commit 1994ef9

Please sign in to comment.