Skip to content
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 8 additions & 12 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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!
```
34 changes: 15 additions & 19 deletions index.js
Original file line number Diff line number Diff line change
@@ -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);
}
Expand Down
2 changes: 1 addition & 1 deletion index.test.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
const { doFile, doString, loadProgram } = require('.');
import { doFile, doString, loadProgram } from '.';

test('doString', () => {
const program = `
Expand Down
5 changes: 1 addition & 4 deletions native/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
29 changes: 9 additions & 20 deletions native/src/do_file.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,24 +13,13 @@ pub fn do_file_sync(mut cx: FunctionContext) -> JsResult<JsValue> {

pub fn do_file_async(mut cx: FunctionContext) -> JsResult<JsValue> {
let program = cx.argument::<JsString>(0)?.value(&mut cx);
let callback = cx.argument::<JsFunction>(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))
}
29 changes: 9 additions & 20 deletions native/src/do_string.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,24 +13,13 @@ pub fn do_string_sync(mut cx: FunctionContext) -> JsResult<JsValue> {

pub fn do_string_async(mut cx: FunctionContext) -> JsResult<JsValue> {
let program = cx.argument::<JsString>(0)?.value(&mut cx);
let callback = cx.argument::<JsFunction>(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))
}
2 changes: 1 addition & 1 deletion native/src/program/entry.rs
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ impl Entry {
} else if let Ok(o) = value.downcast::<JsObject, _>(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
));
Expand Down
2 changes: 2 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading