diff --git a/README.md b/README.md index e8d9627..f627259 100644 --- a/README.md +++ b/README.md @@ -5,24 +5,20 @@ The Lua runtime for Node.js. ## Usage ```js -const { doFile, doString } = require('do-lua'); +import { doFile, doString } from 'do-lua'; const program = ` print("Hello, World!") `; -doString(program).then(() => { - console.log("Done doString"); -}) -doFile('examples/test1.lua').then(() => { - console.log("Done doFile"); -}) +await doString(program); +await doFile('examples/test1.lua'); ``` You cannot use `this` in functions of the passing table on `loadProgram`. Use arrow function instead of that. ```js -const { loadProgram } = require('do-lua'); +import { loadProgram } from 'do-lua'; const state = loadProgram(` obj.ox = 50; @@ -37,8 +33,8 @@ const table = { }; state.setTable('obj', table); -state.run().then((G) => { // G is global table exclusive "package" and "_G" - console.log("ox: ", G.obj.ox); // 50 - console.log("Message: ", message); // Hello, World! -}); +// G is global table exclusive "package" and "_G" +const G = await state.run() +console.log("ox: ", G.obj.ox); // 50 +console.log("Message: ", message); // Hello, World! ``` diff --git a/index.js b/index.js index 094cd62..f02e9d6 100644 --- a/index.js +++ b/index.js @@ -1,28 +1,24 @@ -const native = require('./native'); +import * as native from './native'; -function StateConstructor(program) { - this.program = native.loadProgram(program); -}; +class StateConstructor { + constructor(program) { + this.program = native.loadProgram(program); + } -StateConstructor.prototype.setTable = function setTable(name, table) { - native.setTable(this.program, name, table); -}; + setTable(name, table) { + native.setTable(this.program, name, table); + } -StateConstructor.prototype.run = function run() { - return new Promise((resolve) => native.run(this.program, resolve)); -}; + run() { + return new Promise((resolve) => native.run(this.program, resolve)); + } +} -module.exports = { +exports = { doStringSync: native.doStringSync, - doString(program) { - return new Promise((resolve) => native.doStringAsync(program, resolve)); - }, - + doString: native.doStringAsync, doFileSync: native.doFileSync, - doFile(filename) { - return new Promise((resolve) => native.doFileAsync(filename, resolve)); - }, - + doFile: native.doFileAsync, loadProgram(program) { return new StateConstructor(program); } diff --git a/index.test.js b/index.test.js index 4cb83b0..b6b69d0 100644 --- a/index.test.js +++ b/index.test.js @@ -1,4 +1,4 @@ -const { doFile, doString, loadProgram } = require('.'); +import { doFile, doString, loadProgram } from '.'; test('doString', () => { const program = ` diff --git a/native/Cargo.toml b/native/Cargo.toml index 14946a7..c9037b1 100644 --- a/native/Cargo.toml +++ b/native/Cargo.toml @@ -14,8 +14,5 @@ crate-type = ["cdylib"] [dependencies] lua = { version = "0.0.10" } -neon = { version = "0.10.1", default-features = false, features = [ - "napi-6", - "channel-api", -] } +neon = { version = "1.1.1", default-features = false, features = ["napi-6"] } static_assertions = "1.1.0" diff --git a/native/src/do_file.rs b/native/src/do_file.rs index b2b23bb..fd913e3 100644 --- a/native/src/do_file.rs +++ b/native/src/do_file.rs @@ -13,24 +13,13 @@ pub fn do_file_sync(mut cx: FunctionContext) -> JsResult { pub fn do_file_async(mut cx: FunctionContext) -> JsResult { let program = cx.argument::(0)?.value(&mut cx); - let callback = cx.argument::(1)?.root(&mut cx); - let mut channel = cx.channel(); - channel.unref(&mut cx); - - std::thread::spawn(move || { - let mut state = State::new(); - state.open_libs(); - let status = state.do_file(&program); - - channel.send(move |mut cx| { - let callback = callback.into_inner(&mut cx); - let this = cx.undefined(); - let args = [convert_err(status, &mut state, &mut cx)?]; - - callback.call(&mut cx, this, args)?; - - Ok(()) - }); - }); - Ok(cx.undefined().as_value(&mut cx)) + let promise = cx + .task(move || { + let mut state = State::new(); + state.open_libs(); + let status = state.do_file(&program); + (state, status) + }) + .promise(|mut cx, (mut state, status)| convert_err(status, &mut state, &mut cx)); + Ok(promise.as_value(&mut cx)) } diff --git a/native/src/do_string.rs b/native/src/do_string.rs index 57792ff..5b9c2da 100644 --- a/native/src/do_string.rs +++ b/native/src/do_string.rs @@ -13,24 +13,13 @@ pub fn do_string_sync(mut cx: FunctionContext) -> JsResult { pub fn do_string_async(mut cx: FunctionContext) -> JsResult { let program = cx.argument::(0)?.value(&mut cx); - let callback = cx.argument::(1)?.root(&mut cx); - let mut channel = cx.channel(); - channel.unref(&mut cx); - - std::thread::spawn(move || { - let mut state = State::new(); - state.open_libs(); - let status = state.do_string(&program); - - channel.send(move |mut cx| { - let callback = callback.into_inner(&mut cx); - let this = cx.undefined(); - let args = [convert_err(status, &mut state, &mut cx)?]; - - callback.call(&mut cx, this, args)?; - - Ok(()) - }); - }); - Ok(cx.undefined().as_value(&mut cx)) + let promise = cx + .task(move || { + let mut state = State::new(); + state.open_libs(); + let status = state.do_string(&program); + (state, status) + }) + .promise(|mut cx, (mut state, status)| convert_err(status, &mut state, &mut cx)); + Ok(promise.as_value(&mut cx)) } diff --git a/native/src/program/entry.rs b/native/src/program/entry.rs index 5a9d5ed..bbd2d7b 100644 --- a/native/src/program/entry.rs +++ b/native/src/program/entry.rs @@ -65,7 +65,7 @@ impl Entry { } else if let Ok(o) = value.downcast::(cx) { Entry::Table(Table::from_js(cx, o)?) } else { - return cx.throw_type_error(&format!( + return cx.throw_type_error(format!( "found value of unsupported type on the key: {:?}", key )); diff --git a/package-lock.json b/package-lock.json index 2f95c4b..b5d20e7 100644 --- a/package-lock.json +++ b/package-lock.json @@ -59,6 +59,7 @@ "integrity": "sha512-UlLAnTPrFdNGoFtbSXwcGFQBtQZJCNjaN6hQNP3UPvuNXT1i82N26KL3dZeIpNalWywr9IuQuncaAfUaS1g6sQ==", "dev": true, "license": "MIT", + "peer": true, "dependencies": { "@ampproject/remapping": "^2.2.0", "@babel/code-frame": "^7.27.1", @@ -1284,6 +1285,7 @@ } ], "license": "MIT", + "peer": true, "dependencies": { "caniuse-lite": "^1.0.30001726", "electron-to-chromium": "^1.5.173",