Skip to content

Commit e246d6f

Browse files
authored
fix: Modernize (#21)
2 parents c78150a + 3f47318 commit e246d6f

8 files changed

Lines changed: 46 additions & 77 deletions

File tree

README.md

Lines changed: 8 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -5,24 +5,20 @@ The Lua runtime for Node.js.
55
## Usage
66

77
```js
8-
const { doFile, doString } = require('do-lua');
8+
import { doFile, doString } from 'do-lua';
99

1010
const program = `
1111
print("Hello, World!")
1212
`;
1313

14-
doString(program).then(() => {
15-
console.log("Done doString");
16-
})
17-
doFile('examples/test1.lua').then(() => {
18-
console.log("Done doFile");
19-
})
14+
await doString(program);
15+
await doFile('examples/test1.lua');
2016
```
2117

2218
You cannot use `this` in functions of the passing table on `loadProgram`. Use arrow function instead of that.
2319

2420
```js
25-
const { loadProgram } = require('do-lua');
21+
import { loadProgram } from 'do-lua';
2622

2723
const state = loadProgram(`
2824
obj.ox = 50;
@@ -37,8 +33,8 @@ const table = {
3733
};
3834
state.setTable('obj', table);
3935

40-
state.run().then((G) => { // G is global table exclusive "package" and "_G"
41-
console.log("ox: ", G.obj.ox); // 50
42-
console.log("Message: ", message); // Hello, World!
43-
});
36+
// G is global table exclusive "package" and "_G"
37+
const G = await state.run()
38+
console.log("ox: ", G.obj.ox); // 50
39+
console.log("Message: ", message); // Hello, World!
4440
```

index.js

Lines changed: 15 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,24 @@
1-
const native = require('./native');
1+
import * as native from './native';
22

3-
function StateConstructor(program) {
4-
this.program = native.loadProgram(program);
5-
};
3+
class StateConstructor {
4+
constructor(program) {
5+
this.program = native.loadProgram(program);
6+
}
67

7-
StateConstructor.prototype.setTable = function setTable(name, table) {
8-
native.setTable(this.program, name, table);
9-
};
8+
setTable(name, table) {
9+
native.setTable(this.program, name, table);
10+
}
1011

11-
StateConstructor.prototype.run = function run() {
12-
return new Promise((resolve) => native.run(this.program, resolve));
13-
};
12+
run() {
13+
return new Promise((resolve) => native.run(this.program, resolve));
14+
}
15+
}
1416

15-
module.exports = {
17+
exports = {
1618
doStringSync: native.doStringSync,
17-
doString(program) {
18-
return new Promise((resolve) => native.doStringAsync(program, resolve));
19-
},
20-
19+
doString: native.doStringAsync,
2120
doFileSync: native.doFileSync,
22-
doFile(filename) {
23-
return new Promise((resolve) => native.doFileAsync(filename, resolve));
24-
},
25-
21+
doFile: native.doFileAsync,
2622
loadProgram(program) {
2723
return new StateConstructor(program);
2824
}

index.test.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
const { doFile, doString, loadProgram } = require('.');
1+
import { doFile, doString, loadProgram } from '.';
22

33
test('doString', () => {
44
const program = `

native/Cargo.toml

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,5 @@ crate-type = ["cdylib"]
1414

1515
[dependencies]
1616
lua = { version = "0.0.10" }
17-
neon = { version = "0.10.1", default-features = false, features = [
18-
"napi-6",
19-
"channel-api",
20-
] }
17+
neon = { version = "1.1.1", default-features = false, features = ["napi-6"] }
2118
static_assertions = "1.1.0"

native/src/do_file.rs

Lines changed: 9 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -13,24 +13,13 @@ pub fn do_file_sync(mut cx: FunctionContext) -> JsResult<JsValue> {
1313

1414
pub fn do_file_async(mut cx: FunctionContext) -> JsResult<JsValue> {
1515
let program = cx.argument::<JsString>(0)?.value(&mut cx);
16-
let callback = cx.argument::<JsFunction>(1)?.root(&mut cx);
17-
let mut channel = cx.channel();
18-
channel.unref(&mut cx);
19-
20-
std::thread::spawn(move || {
21-
let mut state = State::new();
22-
state.open_libs();
23-
let status = state.do_file(&program);
24-
25-
channel.send(move |mut cx| {
26-
let callback = callback.into_inner(&mut cx);
27-
let this = cx.undefined();
28-
let args = [convert_err(status, &mut state, &mut cx)?];
29-
30-
callback.call(&mut cx, this, args)?;
31-
32-
Ok(())
33-
});
34-
});
35-
Ok(cx.undefined().as_value(&mut cx))
16+
let promise = cx
17+
.task(move || {
18+
let mut state = State::new();
19+
state.open_libs();
20+
let status = state.do_file(&program);
21+
(state, status)
22+
})
23+
.promise(|mut cx, (mut state, status)| convert_err(status, &mut state, &mut cx));
24+
Ok(promise.as_value(&mut cx))
3625
}

native/src/do_string.rs

Lines changed: 9 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -13,24 +13,13 @@ pub fn do_string_sync(mut cx: FunctionContext) -> JsResult<JsValue> {
1313

1414
pub fn do_string_async(mut cx: FunctionContext) -> JsResult<JsValue> {
1515
let program = cx.argument::<JsString>(0)?.value(&mut cx);
16-
let callback = cx.argument::<JsFunction>(1)?.root(&mut cx);
17-
let mut channel = cx.channel();
18-
channel.unref(&mut cx);
19-
20-
std::thread::spawn(move || {
21-
let mut state = State::new();
22-
state.open_libs();
23-
let status = state.do_string(&program);
24-
25-
channel.send(move |mut cx| {
26-
let callback = callback.into_inner(&mut cx);
27-
let this = cx.undefined();
28-
let args = [convert_err(status, &mut state, &mut cx)?];
29-
30-
callback.call(&mut cx, this, args)?;
31-
32-
Ok(())
33-
});
34-
});
35-
Ok(cx.undefined().as_value(&mut cx))
16+
let promise = cx
17+
.task(move || {
18+
let mut state = State::new();
19+
state.open_libs();
20+
let status = state.do_string(&program);
21+
(state, status)
22+
})
23+
.promise(|mut cx, (mut state, status)| convert_err(status, &mut state, &mut cx));
24+
Ok(promise.as_value(&mut cx))
3625
}

native/src/program/entry.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ impl Entry {
6565
} else if let Ok(o) = value.downcast::<JsObject, _>(cx) {
6666
Entry::Table(Table::from_js(cx, o)?)
6767
} else {
68-
return cx.throw_type_error(&format!(
68+
return cx.throw_type_error(format!(
6969
"found value of unsupported type on the key: {:?}",
7070
key
7171
));

package-lock.json

Lines changed: 2 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)