-
Notifications
You must be signed in to change notification settings - Fork 287
JsSymbol primitive #761
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
JsSymbol primitive #761
Changes from 3 commits
a110490
53ad833
23a797c
cf740fd
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,85 @@ | ||
| use crate::context::Context; | ||
| use crate::handle::{Handle, Managed}; | ||
| use crate::types::internal::ValueInternal; | ||
| use crate::types::utf8::Utf8; | ||
| use crate::types::{Env, JsString, Value}; | ||
|
|
||
| use neon_runtime::raw; | ||
|
|
||
| /// A JavaScript symbol primitive value. | ||
| #[repr(C)] | ||
| #[derive(Clone, Copy)] | ||
| pub struct JsSymbol(raw::Local); | ||
|
|
||
| impl JsSymbol { | ||
| /// Create a new symbol. | ||
| /// Equivalent to calling `Symbol()` in JavaScript | ||
| pub fn new<'a, C: Context<'a>>(cx: &mut C) -> Handle<'a, JsSymbol> { | ||
| JsSymbol::new_internal(cx.env(), None) | ||
| } | ||
|
|
||
| /// Create a new symbol with a description. | ||
| /// Equivalent to calling `Symbol(description)` in JavaScript | ||
| pub fn with_description<'a, C: Context<'a>>( | ||
| cx: &mut C, | ||
| desc: Handle<'a, JsString>, | ||
| ) -> Handle<'a, JsSymbol> { | ||
| JsSymbol::new_internal(cx.env(), Some(desc)) | ||
| } | ||
|
|
||
| /// Get the optional symbol description, where `None` represents an undefined description. | ||
| pub fn description<'a, C: Context<'a>>(self, cx: &mut C) -> Option<Handle<'a, JsString>> { | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'd prefer if this were built into Node-API, but looks like it isn't. The best I can tell reading the ECMAScript spec, this property is guaranteed to exist and it's impossible to overwrite. @dherman is that correct?
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. FYI, when I first wrote up the RFC, node@10 was still in maintenance LTS, and symbol.prototype.description was unsupported. With this implementation, if I downgrade to 10.24, the description tests fail both the node and rust getter with:
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The good news is we no longer support Node 10 and can make that simplification. |
||
| let env = cx.env().to_raw(); | ||
| let (desc_ptr, desc_len) = Utf8::from("description").into_small_unwrap().lower(); | ||
|
||
|
|
||
| unsafe { | ||
| let mut local = std::mem::zeroed(); | ||
| if !neon_runtime::object::get_string(env, &mut local, self.to_raw(), desc_ptr, desc_len) | ||
| { | ||
| return None; | ||
| } | ||
|
|
||
| if neon_runtime::tag::is_string(env, local) { | ||
| Some(Handle::new_internal(JsString(local))) | ||
| } else { | ||
| None | ||
| } | ||
| } | ||
| } | ||
|
|
||
| pub(crate) fn new_internal<'a>( | ||
| env: Env, | ||
| desc: Option<Handle<'a, JsString>>, | ||
| ) -> Handle<'a, JsSymbol> { | ||
| unsafe { | ||
| let desc_local = match desc { | ||
| None => std::ptr::null_mut(), | ||
| Some(h) => h.to_raw(), | ||
| }; | ||
| let sym_local = neon_runtime::primitive::symbol(env.to_raw(), desc_local); | ||
| Handle::new_internal(JsSymbol(sym_local)) | ||
| } | ||
| } | ||
| } | ||
|
|
||
| impl Value for JsSymbol {} | ||
|
|
||
| impl Managed for JsSymbol { | ||
| fn to_raw(self) -> raw::Local { | ||
| self.0 | ||
| } | ||
|
|
||
| fn from_raw(_: Env, h: raw::Local) -> Self { | ||
| JsSymbol(h) | ||
| } | ||
| } | ||
|
|
||
| impl ValueInternal for JsSymbol { | ||
| fn name() -> String { | ||
| "symbol".to_string() | ||
| } | ||
|
|
||
| fn is_typeof<Other: Value>(env: Env, other: Other) -> bool { | ||
| unsafe { neon_runtime::tag::is_symbol(env.to_raw(), other.to_raw()) } | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,42 @@ | ||
| const addon = require('..'); | ||
| const { assert } = require('chai'); | ||
|
|
||
| describe('JsSymbol', function () { | ||
| it('should return a JsSymbol with a description built with the context helper in Rust', function () { | ||
| const sym = addon.return_js_symbol_from_context_helper(); | ||
| assert.typeOf(sym, 'symbol'); | ||
| assert.equal(sym.description, 'neon:context_helper'); | ||
| }); | ||
|
|
||
| it('should return a JsSymbol with a description built in Rust', function () { | ||
| const description = 'neon:description' | ||
| const sym = addon.return_js_symbol_with_description(description); | ||
| assert.typeOf(sym, 'symbol'); | ||
| assert.equal(sym.description, description); | ||
| }); | ||
|
|
||
| it('should return a JsSymbol without a description built in Rust', function () { | ||
| const sym = addon.return_js_symbol(); | ||
| assert.typeOf(sym, 'symbol'); | ||
| assert.equal(sym.description, undefined); | ||
| }); | ||
|
|
||
| it('should read the description property in Rust', function () { | ||
| const sym = Symbol('neon:description'); | ||
| const description = addon.read_js_symbol_description(sym); | ||
| assert.equal(description, 'neon:description'); | ||
| }); | ||
|
|
||
| it('should read an undefined description property in Rust', function () { | ||
| const sym = Symbol(); | ||
| const description = addon.read_js_symbol_description(sym); | ||
| assert.equal(description, undefined); | ||
| }); | ||
|
|
||
| it('accepts and returns symbols', function () { | ||
| const symDesc = Symbol('neon:description'); | ||
| const symNoDesc = Symbol(); | ||
| assert.equal(addon.accept_and_return_js_symbol(symDesc), symDesc); | ||
| assert.equal(addon.accept_and_return_js_symbol(symNoDesc), symNoDesc); | ||
| }); | ||
| }); |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,27 @@ | ||
| use neon::prelude::*; | ||
|
|
||
| pub fn return_js_symbol_from_context_helper(mut cx: FunctionContext) -> JsResult<JsSymbol> { | ||
| Ok(cx.symbol("neon:context_helper")) | ||
| } | ||
|
|
||
| pub fn return_js_symbol_with_description(mut cx: FunctionContext) -> JsResult<JsSymbol> { | ||
| let description: Handle<JsString> = cx.argument(0)?; | ||
| Ok(JsSymbol::with_description(&mut cx, description)) | ||
| } | ||
|
|
||
| pub fn return_js_symbol(mut cx: FunctionContext) -> JsResult<JsSymbol> { | ||
chrisbajorin marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| Ok(JsSymbol::new(&mut cx)) | ||
| } | ||
|
|
||
| pub fn read_js_symbol_description(mut cx: FunctionContext) -> JsResult<JsValue> { | ||
| let symbol: Handle<JsSymbol> = cx.argument(0)?; | ||
| symbol | ||
| .description(&mut cx) | ||
| .map(|v| Ok(v.upcast())) | ||
| .unwrap_or_else(|| Ok(cx.undefined().upcast())) | ||
| } | ||
|
|
||
| pub fn accept_and_return_js_symbol(mut cx: FunctionContext) -> JsResult<JsSymbol> { | ||
| let sym: Handle<JsSymbol> = cx.argument(0)?; | ||
| Ok(sym) | ||
| } | ||
Uh oh!
There was an error while loading. Please reload this page.