diff --git a/.eslintrc.yml b/.eslintrc.yml index fc8a1c03..3ff3d348 100644 --- a/.eslintrc.yml +++ b/.eslintrc.yml @@ -203,11 +203,11 @@ rules: overrides: - files: - - ./**/*.js + - ./test_module/**/*.js plugins: - '@typescript-eslint' parserOptions: - project: ./tsconfig.json + project: ./test_module/tsconfig.json rules: '@typescript-eslint/prefer-nullish-coalescing': 0 '@typescript-eslint/prefer-optional-chain': 0 diff --git a/test_module/__test__/class.spec.js b/test_module/__test__/class.spec.js new file mode 100644 index 00000000..a506e663 --- /dev/null +++ b/test_module/__test__/class.spec.js @@ -0,0 +1,13 @@ +const test = require('ava') + +const bindings = require('../index.node') + +test('should create class', (t) => { + const TestClass = bindings.createTestClass() + const fixture = 20 + const testClass = new TestClass(fixture) + t.is(testClass.count, fixture) + const add = 101 + testClass.addCount(add) + t.is(testClass.count, fixture + add) +}) diff --git a/test_module/src/class.rs b/test_module/src/class.rs new file mode 100644 index 00000000..f7e3af8c --- /dev/null +++ b/test_module/src/class.rs @@ -0,0 +1,34 @@ +use std::convert::TryInto; + +use napi::{CallContext, JsFunction, JsNumber, JsObject, JsUndefined, Property, Result}; + +#[js_function(1)] +pub fn create_test_class(ctx: CallContext) -> Result { + let add_count_method = Property::new("addCount").with_method(add_count); + let properties = vec![add_count_method]; + ctx + .env + .define_class("TestClass", test_class_constructor, properties) +} + +#[js_function(1)] +pub fn test_class_constructor(mut ctx: CallContext) -> Result { + let count = ctx.get::(0)?; + ctx + .this + .set_named_property("count", ctx.env.create_int32(count.try_into()?)?)?; + ctx.env.get_undefined() +} + +#[js_function(1)] +pub fn add_count(mut ctx: CallContext) -> Result { + let add: i32 = ctx.get::(0)?.try_into()?; + let count: i32 = ctx + .this + .get_named_property::("count")? + .try_into()?; + ctx + .this + .set_named_property("count", ctx.env.create_int32(count + add)?)?; + ctx.env.get_undefined() +} diff --git a/test_module/src/lib.rs b/test_module/src/lib.rs index 88c9a900..e54fa5cc 100644 --- a/test_module/src/lib.rs +++ b/test_module/src/lib.rs @@ -15,6 +15,7 @@ mod napi5; mod tokio_rt; mod buffer; +mod class; mod either; mod external; mod function; @@ -56,6 +57,7 @@ fn init(module: &mut Module) -> Result<()> { module.create_named_method("testCallFunctionWithThis", call_function_with_this)?; module.create_named_method("eitherNumberString", either_number_string)?; module.create_named_method("dynamicArgumentLength", dynamic_argument_length)?; + module.create_named_method("createTestClass", class::create_test_class)?; #[cfg(napi4)] module.create_named_method("testExecuteTokioReadfile", test_execute_tokio_readfile)?; #[cfg(napi4)] diff --git a/test_module/tsconfig.json b/test_module/tsconfig.json new file mode 100644 index 00000000..1ee419b4 --- /dev/null +++ b/test_module/tsconfig.json @@ -0,0 +1,8 @@ +{ + "extends": "../tsconfig.json", + "compilerOptions": { + "outDir": "./dist", + "allowJs": true + }, + "include": ["."] +}