1
0
Fork 0
mirror of https://github.com/denoland/deno.git synced 2024-11-25 15:29:32 -05:00
denoland-deno/cli/napi
Matt Mastracci d51fda9e14
fix(ext/napi): don't close handle scopes in NAPI as the pointers are invalid (#21629)
`napi_open_handle_scope` was returning a bogus handle_scope and we were
trying to close it in `napi_close_handle_scope`.

This is a bit of a challenge to test, but the following testcase comes
from #21601 and appears to be fixed by this.

```
import { decode } from "https://deno.land/std@0.209.0/encoding/base64.ts";
import sharp from "npm:sharp";

const img = 'iVBORw0KGgoAAAANSUhEUgAAAQAAAAEAAQMAAABmvDolAAAAA1BMVEWq09/P7Lz1AAAAH0lEQVRoge3BAQ0AAADCoPdPbQ43oAAAAAAAAAAAvg0hAAABmmDh1QAAAABJRU5ErkJggg==';
Deno.test("async", async () => {
  const id = setTimeout(() => Deno.exit(1), 1000);
  await sharp(decode(img)).toBuffer();
  await sharp(decode(img)).toBuffer();
  clearTimeout(id);
});
```
2023-12-18 08:48:52 -07:00
..
sym 1.39.0 (#21560) 2023-12-13 13:24:13 -07:00
async.rs chore(node-api): reuse SendPtr (#21567) 2023-12-14 15:46:57 -07:00
env.rs fix(napi): clear currently registering module slot (#19249) 2023-05-26 10:10:17 +05:30
generated_symbol_exports_list_linux.def refactor: move generated_symbol_exports_list_* files to napi folder (#16822) 2022-11-25 18:49:02 -05:00
generated_symbol_exports_list_macos.def refactor: move generated_symbol_exports_list_* files to napi folder (#16822) 2022-11-25 18:49:02 -05:00
generated_symbol_exports_list_windows.def refactor: move generated_symbol_exports_list_* files to napi folder (#16822) 2022-11-25 18:49:02 -05:00
js_native_api.rs fix(ext/napi): don't close handle scopes in NAPI as the pointers are invalid (#21629) 2023-12-18 08:48:52 -07:00
mod.rs chore(napi): Remove unstable libuv methods (#17416) 2023-01-14 13:02:42 +00:00
README.md chore: fix typos (#19572) 2023-06-26 09:10:27 -04:00
threadsafe_functions.rs chore(node-api): reuse SendPtr (#21567) 2023-12-14 15:46:57 -07:00
util.rs chore: update copyright year to 2023 (#17247) 2023-01-02 21:00:42 +00:00

napi

This directory contains source for Deno's Node-API implementation. It depends on napi_sym and deno_napi.

Adding a new function

Add the symbol name to cli/napi_sym/symbol_exports.json.

{
  "symbols": [
    ...
    "napi_get_undefined",
-   "napi_get_null"
+   "napi_get_null",
+   "napi_get_boolean"
  ]
}

Determine where to place the implementation. napi_get_boolean is related to JS values so we will place it in js_native_api.rs. If something is not clear, just create a new file module.

See napi_sym for writing the implementation:

#[napi_sym::napi_sym]
pub fn napi_get_boolean(
  env: *mut Env,
  value: bool,
  result: *mut napi_value,
) -> Result {
  // ...
  Ok(())
}

Update the generated symbol lists using the script:

deno run --allow-write tools/napi/generate_symbols_lists.js

Add a test in /test_napi. You can also refer to Node.js test suite for Node-API.

// test_napi/boolean_test.js
import { assertEquals, loadTestLibrary } from "./common.js";
const lib = loadTestLibrary();
Deno.test("napi get boolean", function () {
  assertEquals(lib.test_get_boolean(true), true);
  assertEquals(lib.test_get_boolean(false), false);
});
// test_napi/src/boolean.rs

use napi_sys::Status::napi_ok;
use napi_sys::ValueType::napi_boolean;
use napi_sys::*;

extern "C" fn test_boolean(
  env: napi_env,
  info: napi_callback_info,
) -> napi_value {
  let (args, argc, _) = crate::get_callback_info!(env, info, 1);
  assert_eq!(argc, 1);

  let mut ty = -1;
  assert!(unsafe { napi_typeof(env, args[0], &mut ty) } == napi_ok);
  assert_eq!(ty, napi_boolean);

  // Use napi_get_boolean here...

  value
}

pub fn init(env: napi_env, exports: napi_value) {
  let properties = &[crate::new_property!(env, "test_boolean\0", test_boolean)];

  unsafe {
    napi_define_properties(env, exports, properties.len(), properties.as_ptr())
  };
}
// test_napi/src/lib.rs

+ mod boolean;

...

#[no_mangle]
unsafe extern "C" fn napi_register_module_v1(
  env: napi_env,
  exports: napi_value,
) -> napi_value {
  ...
+ boolean::init(env, exports);

  exports
}

Run the test using cargo test -p test_napi.