Skip to content

Commit

Permalink
Add CommonJS testing
Browse files Browse the repository at this point in the history
  • Loading branch information
Doridian committed Mar 2, 2024
1 parent 2cefde8 commit 2ebe3b0
Show file tree
Hide file tree
Showing 8 changed files with 94 additions and 99 deletions.
3 changes: 1 addition & 2 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,7 @@
/dist
/src/luajs.*
!/src/luajs.c
!/src/luajs.d.mts
!/src/luajs.d.cts
!/src/luajs.d.ts
/src/*.[oa]
/tmp
node_modules
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
"type": "module",
"scripts": {
"clean": "emmake make clean",
"build": "mkdir -p dist && cp -f src/luajs.d.* dist/ && cd src/ && tsc && cd .. && tsc && emmake make install",
"build": "mkdir -p dist && cp -f src/luajs.d.ts dist/luajs.d.mts && cp -f src/luajs.d.ts dist/luajs.d.cts && cd src/ && tsc && cd .. && tsc && emmake make install",
"lint": "echo 'Lint not set up yet'",
"fmt": "echo 'Format not set up yet'",
"test": "npm run build && npm run testOnly",
Expand Down
36 changes: 0 additions & 36 deletions src/luajs.d.mts

This file was deleted.

72 changes: 36 additions & 36 deletions src/luajs.d.cts → src/luajs.d.ts
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>;
39 changes: 39 additions & 0 deletions test/basics_cjs.cts
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');
});
31 changes: 13 additions & 18 deletions util/loader_cjs.cts
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';
8 changes: 3 additions & 5 deletions util/loader_mjs.mts
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';
2 changes: 1 addition & 1 deletion util/testrunner.mts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { run } from 'node:test';
import { globSync } from 'glob';

const runStream = run({
files: globSync("test/**/*.mjs"),
files: globSync("test/**/*.mjs").concat(globSync("test/**/*.cjs")),
})
let outputStream: {
pipe: (stream: NodeJS.WritableStream) => void;
Expand Down

0 comments on commit 2ebe3b0

Please sign in to comment.