Skip to content

Commit 9615e8b

Browse files
committed
Support function classes, arg variable
1 parent 0a2853a commit 9615e8b

File tree

5 files changed

+56
-24
lines changed

5 files changed

+56
-24
lines changed

bin/demoon

+2-2
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,6 @@
33
const { start } = require('../src/index');
44

55
const snippets = process.argv.splice(2);
6-
const [entryFile] = snippets
6+
const [entryFile, ...arg] = snippets
77

8-
start(entryFile)
8+
start(entryFile, arg)

package-lock.json

+17-17
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "demoon",
3-
"version": "0.0.6",
3+
"version": "0.0.7",
44
"description": "Lua + Node",
55
"main": "src/index.js",
66
"bin": {
@@ -13,6 +13,6 @@
1313
"lua"
1414
],
1515
"dependencies": {
16-
"wasmoon": "1.8.2"
16+
"wasmoon": "1.11.0"
1717
}
1818
}

src/index.js

+6-3
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
1-
const { LuaFactory } = require('wasmoon')
1+
const { LuaFactory, LuaMultiReturn } = require('wasmoon')
22
const path = require('path')
3-
const fs = require('fs').promises
3+
const FunctionClassTypeExtension = require('./legacyclasses')
4+
const fs = require('fs/promises')
45

5-
const start = async (entryFile) => {
6+
const start = async (entryFile, arg) => {
67
const factory = new LuaFactory(undefined, process.env)
78

89
const fullEntryFile = path.resolve(process.cwd(), entryFile)
@@ -13,6 +14,8 @@ const start = async (entryFile) => {
1314

1415
const engine = await factory.createEngine({ injectObjects: true })
1516

17+
engine.global.registerTypeExtension(10, new FunctionClassTypeExtension)
18+
engine.global.set('arg', arg)
1619
engine.global.set('typeof', value => typeof value)
1720
engine.global.set('instanceof', (value, type) => value instanceof type)
1821
engine.global.set('new', (constructor, ...args) => new constructor(...args))

src/legacyclasses.js

+29
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
const { decorateProxy, LuaTypeExtension } = require("wasmoon");
2+
3+
const functionClasses = [
4+
Buffer,
5+
Object,
6+
Array,
7+
String,
8+
Number,
9+
]
10+
11+
module.exports = class extends LuaTypeExtension {
12+
constructor(thread, injectObject) {
13+
super(thread, 'js_functionclass')
14+
}
15+
16+
pushValue(thread, { target, options }) {
17+
// If is a function not bounded yet
18+
if (typeof target === 'function' && !options?.proxy) {
19+
const isLegacyNativeClass = functionClasses.includes(target)
20+
// If it has a prototype, probably is something important
21+
const hasPrototype = target.prototype && Object.keys(target.prototype).length > 0
22+
23+
if (isLegacyNativeClass || hasPrototype) {
24+
thread.pushValue(decorateProxy(target, { proxy: true }))
25+
return true
26+
}
27+
}
28+
}
29+
}

0 commit comments

Comments
 (0)