Skip to content
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

feat(napi-derive): allow injecting this in class method #1221

Merged
merged 1 commit into from Jul 6, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
58 changes: 36 additions & 22 deletions crates/backend/src/codegen/fn.rs
Expand Up @@ -12,7 +12,7 @@ impl TryToTokens for NapiFn {
let intermediate_ident = get_intermediate_ident(&name_str);
let args_len = self.args.len();

let (arg_conversions, arg_names) = self.gen_arg_conversions();
let (arg_conversions, arg_names) = self.gen_arg_conversions()?;
let receiver = self.gen_fn_receiver();
let receiver_ret_name = Ident::new("_ret", Span::call_site());
let ret = self.gen_fn_return(&receiver_ret_name);
Expand Down Expand Up @@ -93,7 +93,7 @@ impl TryToTokens for NapiFn {
}

impl NapiFn {
fn gen_arg_conversions(&self) -> (Vec<TokenStream>, Vec<TokenStream>) {
fn gen_arg_conversions(&self) -> BindgenResult<(Vec<TokenStream>, Vec<TokenStream>)> {
let mut arg_conversions = vec![];
let mut args = vec![];

Expand All @@ -117,7 +117,7 @@ impl NapiFn {
}

let mut skipped_arg_count = 0;
self.args.iter().enumerate().for_each(|(i, arg)| {
for (i, arg) in self.args.iter().enumerate() {
let i = i - skipped_arg_count;
let ident = Ident::new(&format!("arg{}", i), Span::call_site());

Expand All @@ -127,27 +127,41 @@ impl NapiFn {
args.push(quote! { napi::bindgen_prelude::Env::from(env) });
skipped_arg_count += 1;
} else {
if self.parent.is_some() {
if let syn::Type::Path(path) = path.ty.as_ref() {
if let Some(p) = path.path.segments.last() {
if p.ident == "Reference" {
if let syn::PathArguments::AngleBracketed(
syn::AngleBracketedGenericArguments { args: angle_bracketed_args, .. },
) = &p.arguments
let is_in_class = self.parent.is_some();
if let syn::Type::Path(path) = path.ty.as_ref() {
if let Some(p) = path.path.segments.last() {
if p.ident == "Reference" {
if !is_in_class {
bail_span!(p, "`Reference` is only allowed in class methods");
}
if let syn::PathArguments::AngleBracketed(syn::AngleBracketedGenericArguments {
args: angle_bracketed_args,
..
}) = &p.arguments
{
if let Some(syn::GenericArgument::Type(syn::Type::Path(path))) =
angle_bracketed_args.first()
{
if let Some(syn::GenericArgument::Type(syn::Type::Path(path))) = angle_bracketed_args.first() {
if let Some(p) = path.path.segments.first() {
if p.ident == *self.parent.as_ref().unwrap() {
args.push(
quote! { napi::bindgen_prelude::Reference::from_value_ptr(this_ptr as *mut std::ffi::c_void, env)? },
);
skipped_arg_count += 1;
return;
}
}
if let Some(p) = path.path.segments.first() {
if p.ident == *self.parent.as_ref().unwrap() {
args.push(
quote! { napi::bindgen_prelude::Reference::from_value_ptr(this_ptr as *mut std::ffi::c_void, env)? },
);
skipped_arg_count += 1;
continue;
}
}
}
}
} else if p.ident == "This" {
if !is_in_class {
bail_span!(p, "`This` is only allowed in class methods");
}
args.push(
quote! { <napi::bindgen_prelude::This as napi::NapiValue>::from_raw_unchecked(env, cb.this) },
);
skipped_arg_count += 1;
continue;
}
}
}
Expand All @@ -160,9 +174,9 @@ impl NapiFn {
args.push(quote! { #ident });
}
}
});
}

(arg_conversions, args)
Ok((arg_conversions, args))
}

fn gen_ty_arg_conversion(
Expand Down
1 change: 1 addition & 0 deletions crates/backend/src/typegen.rs
Expand Up @@ -214,6 +214,7 @@ static KNOWN_TYPES: Lazy<HashMap<&'static str, &'static str>> = Lazy::new(|| {
("unknown", "unknown"),
("Unknown", "unknown"),
("JsUnknown", "unknown"),
("This", "this")
]);

map
Expand Down
4 changes: 3 additions & 1 deletion crates/napi/src/bindgen_runtime/js_values/class.rs
Expand Up @@ -2,7 +2,9 @@ use std::any::type_name;
use std::ops::{Deref, DerefMut};
use std::ptr;

use crate::{bindgen_runtime::FromNapiValue, check_status, sys, NapiRaw};
use crate::{bindgen_runtime::FromNapiValue, check_status, sys, JsObject, NapiRaw};

pub type This = JsObject;

pub struct ClassInstance<T: 'static> {
pub value: sys::napi_value,
Expand Down
1 change: 1 addition & 0 deletions examples/napi/__test__/typegen.spec.ts.md
Expand Up @@ -254,6 +254,7 @@ Generated by [AVA](https://avajs.dev).
static newRaph(): NinjaTurtle␊
getMaskColor(): string␊
getName(): string␊
returnThis(this: this): this␊
}␊
export type JsAssets = Assets␊
export class Assets {␊
Expand Down
Binary file modified examples/napi/__test__/typegen.spec.ts.snap
Binary file not shown.
3 changes: 3 additions & 0 deletions examples/napi/__test__/values.spec.ts
Expand Up @@ -21,6 +21,7 @@ import {
getCwd,
Animal,
Kind,
NinjaTurtle,
ClassWithFactory,
CustomNumEnum,
Context,
Expand Down Expand Up @@ -173,6 +174,8 @@ test('class', (t) => {
t.is(dog.type, Kind.Cat)
const assets = new Assets()
t.is(assets.get(1)?.filePath, 1)
const turtle = NinjaTurtle.newRaph()
t.is(turtle.returnThis(), turtle)
})

test('class factory', (t) => {
Expand Down
1 change: 1 addition & 0 deletions examples/napi/index.d.ts
Expand Up @@ -244,6 +244,7 @@ export class NinjaTurtle {
static newRaph(): NinjaTurtle
getMaskColor(): string
getName(): string
returnThis(this: this): this
}
export type JsAssets = Assets
export class Assets {
Expand Down
7 changes: 6 additions & 1 deletion examples/napi/src/class.rs
@@ -1,5 +1,5 @@
use napi::{
bindgen_prelude::{Buffer, ClassInstance},
bindgen_prelude::{Buffer, ClassInstance, This},
Env, Result,
};

Expand Down Expand Up @@ -229,6 +229,11 @@ impl NinjaTurtle {
pub fn get_name(&self) -> &str {
self.name.as_str()
}

#[napi]
pub fn return_this(&self, this: This) -> This {
this
}
}

#[napi(js_name = "Assets")]
Expand Down