Skip to content

Commit 3f47318

Browse files
committed
Update to ES modules
1 parent ece6708 commit 3f47318

3 files changed

Lines changed: 22 additions & 24 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: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,20 @@
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,
1719
doString: native.doStringAsync,
1820
doFileSync: native.doFileSync,

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 = `

0 commit comments

Comments
 (0)