-
Notifications
You must be signed in to change notification settings - Fork 12
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
8 changed files
with
94 additions
and
99 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,36 +1,36 @@ | ||
export class LuaReference { | ||
unref(): void; | ||
push(state?: number): void | ||
getmetatable(): void | ||
setmetatable(): void; | ||
} | ||
|
||
export class LuaFunction extends LuaReference { | ||
getClosure(): (...args: unknown[]) => Promise<unknown[]>; | ||
call(...args: unknown[]): Promise<unknown>; | ||
} | ||
|
||
export class LuaTable extends LuaReference { | ||
set(key: unknown, value:unknown): void; | ||
get(key: unknown): unknown; | ||
toObject(recurse: boolean, unrefAll: boolean, maxDepth?: number): Record<string, unknown> | unknown[]; | ||
} | ||
|
||
export class LuaState { | ||
open(): Promise<void>; | ||
getTop(): void; | ||
unrefAll(): void; | ||
close():void; | ||
run(code: string, blockName?: string): Promise<unknown[]>; | ||
getGlobalTable(): LuaTable; | ||
createTable(): LuaTable; | ||
loadDocumentScripts(doc: Document): Promise<void>; | ||
listenForScripts(doc: Document): void; | ||
enableLuaScriptTags(doc: Document): Promise<void>; | ||
} | ||
|
||
export class LuaJS { | ||
newState(): Promise<LuaState>; | ||
} | ||
|
||
export default function(module: Partial<LuaJS>): Promise<void>; | ||
export class LuaReference { | ||
unref(): void; | ||
push(state?: number): void | ||
getmetatable(): void | ||
setmetatable(): void; | ||
} | ||
|
||
export class LuaFunction extends LuaReference { | ||
getClosure(): (...args: unknown[]) => Promise<unknown[]>; | ||
call(...args: unknown[]): Promise<unknown>; | ||
} | ||
|
||
export class LuaTable extends LuaReference { | ||
set(key: unknown, value:unknown): void; | ||
get(key: unknown): unknown; | ||
toObject(recurse: boolean, unrefAll: boolean, maxDepth?: number): Record<string, unknown> | unknown[]; | ||
} | ||
|
||
export class LuaState { | ||
open(): Promise<void>; | ||
getTop(): void; | ||
unrefAll(): void; | ||
close():void; | ||
run(code: string, blockName?: string): Promise<unknown[]>; | ||
getGlobalTable(): LuaTable; | ||
createTable(): LuaTable; | ||
loadDocumentScripts(doc: Document): Promise<void>; | ||
listenForScripts(doc: Document): void; | ||
enableLuaScriptTags(doc: Document): Promise<void>; | ||
} | ||
|
||
export class LuaJS { | ||
newState(): Promise<LuaState>; | ||
} | ||
|
||
export default function(module?: Partial<LuaJS>): Promise<LuaJS>; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
import test from 'node:test'; | ||
import assert from 'node:assert'; | ||
|
||
import { LuaJS, LuaFunction, LuaTable } from '../util/loader_cjs.cjs'; | ||
|
||
function convertBack(ret: unknown[]): [string, unknown] { | ||
return [ret[0] as string, (ret[1] as LuaTable).toObject(true, true)]; | ||
} | ||
|
||
test('Can run basic Lua code (CommonJS)', async () => { | ||
const L = await LuaJS.newState(); | ||
const ret = await L.run('return 1 + 2, "hello"'); | ||
assert.deepEqual(ret, [3, 'hello']); | ||
}); | ||
|
||
test('Can pass JS types to Lua correctly (CommonJS)', async () => { | ||
const L = await LuaJS.newState(); | ||
|
||
const ret = await L.run('return function(a) return type(a), a end'); | ||
const retConvert = await L.run('return function(a) a = a:toTable(true, 10); return type(a), a end'); | ||
const func = (ret[0] as LuaFunction).getClosure(); | ||
const funcConvert = (retConvert[0] as LuaFunction).getClosure(); | ||
|
||
assert.deepEqual(await func('hello world'), ['string', 'hello world']); | ||
assert.deepEqual(await func(13), ['number', 13]); | ||
assert.deepEqual(await func([1,2,3]), ['userdata', [1,2,3]]); | ||
|
||
// Some fairly simply objects | ||
assert.deepEqual(convertBack(await funcConvert([1,2,true,undefined,null,,3])), ['table', [1,2,true,,,,3]]); | ||
assert.deepEqual(convertBack(await funcConvert({'a': 1, 'b': '2', 'c': true, 'd': undefined, 'e': null})), ['table', {'a': 1, 'b': '2', 'c': true}]); | ||
|
||
// Some more nested things | ||
assert.deepEqual(convertBack(await funcConvert({'a': 1, 'b': [4,{'x':5,'y':[6,9,42]},6], 'c': true})), ['table', {'a': 1, 'b': [4,{'x':5,'y':[6,9,42]},6], 'c': true}]); | ||
}); | ||
|
||
test('allows awaiting JS promises (CommonJS)', async () => { | ||
const L = await LuaJS.newState(); | ||
await L.run('return js.await'); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,18 +1,13 @@ | ||
import type { LuaState } from '../dist/luajs.cjs'; | ||
|
||
class LuaJS { | ||
public async newState(): Promise<LuaState> { | ||
await loadPromise; | ||
return (Module as LuaJS).newState(); | ||
} | ||
} | ||
|
||
const Module = {}; | ||
const loadPromise = (async () => { | ||
const { default : moduleCtor } = await import('../dist/luajs.mjs'); | ||
await moduleCtor(Module); | ||
})(); | ||
|
||
const luaJS = new LuaJS(); | ||
export { luaJS as LuaJS }; | ||
export type { LuaTable, LuaFunction, LuaState } from '../dist/luajs.cjs'; | ||
const loadPromise = (async () => { | ||
const { default : moduleCtor } = await import('../dist/luajs.mjs'); | ||
return await moduleCtor(); | ||
})(); | ||
|
||
const LuaJS = { | ||
newState: async () => { | ||
const Module = await loadPromise; | ||
return Module.newState(); | ||
} | ||
}; | ||
export { LuaJS }; | ||
export type { LuaTable, LuaFunction, LuaState } from '../dist/luajs.cjs'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,5 @@ | ||
import { default as moduleCtor, LuaJS } from '../dist/luajs.mjs'; | ||
import { default as moduleCtor } from '../dist/luajs.mjs'; | ||
|
||
const Module = {}; | ||
await moduleCtor(Module); | ||
const LuaJSInstance = Module as LuaJS; | ||
export { LuaJSInstance as LuaJS }; | ||
const LuaJS = await moduleCtor(); | ||
export { LuaJS }; | ||
export type { LuaTable, LuaFunction, LuaState } from '../dist/luajs.mjs'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters