@cbrewster/luna
Advanced tools
Comparing version 0.0.9 to 0.0.10
@@ -6,2 +6,3 @@ declare type LuaValue = null | string | number | LuaTable; | ||
doString(code: string): Promise<LuaValue>; | ||
newTable(): LuaTable; | ||
close(): void; | ||
@@ -11,3 +12,5 @@ } | ||
table: any; | ||
constructor(table: any); | ||
constructor(table: null | any); | ||
get(key: LuaValue): LuaValue; | ||
set(key: LuaValue, value: LuaValue): LuaValue; | ||
forEach(callback: (key: LuaValue, value: LuaValue) => void): void; | ||
@@ -14,0 +17,0 @@ toString(): string; |
@@ -34,5 +34,14 @@ var __defProp = Object.defineProperty; | ||
var binding_path = find((0, import_path.resolve)((0, import_path.join)(__dirname, "../package.json"))); | ||
var { luaNew, luaClose, luaDoString, luaTableForEach, luaTableToString } = require(binding_path); | ||
var { | ||
luaNew, | ||
luaClose, | ||
luaDoString, | ||
luaNewTable, | ||
luaTableGet, | ||
luaTableSet, | ||
luaTableForEach, | ||
luaTableToString | ||
} = require(binding_path); | ||
var luaDoStringAsync = (0, import_util.promisify)(luaDoString); | ||
function convertLua(val) { | ||
function wrapLua(val) { | ||
if (val == null) { | ||
@@ -46,2 +55,11 @@ return null; | ||
} | ||
function unwrapLua(val) { | ||
if (val == null) { | ||
return null; | ||
} | ||
if (val instanceof LuaTable) { | ||
return val.table; | ||
} | ||
return val; | ||
} | ||
var Lua = class { | ||
@@ -52,4 +70,7 @@ constructor() { | ||
async doString(code) { | ||
return convertLua(await luaDoStringAsync.call(this.lua, code)); | ||
return wrapLua(await luaDoStringAsync.call(this.lua, code)); | ||
} | ||
newTable() { | ||
return new LuaTable(luaNewTable.call(this.lua)); | ||
} | ||
close() { | ||
@@ -63,5 +84,11 @@ luaClose.call(this.lua); | ||
} | ||
get(key) { | ||
return wrapLua(luaTableGet.call(this.table, unwrapLua(key))); | ||
} | ||
set(key, value) { | ||
return wrapLua(luaTableSet.call(this.table, unwrapLua(key), unwrapLua(value))); | ||
} | ||
forEach(callback) { | ||
luaTableForEach.call(this.table, (key, value) => { | ||
callback(convertLua(key), convertLua(value)); | ||
callback(wrapLua(key), wrapLua(value)); | ||
}); | ||
@@ -68,0 +95,0 @@ } |
const { Lua, LuaTable } = require('../'); | ||
(async () => { | ||
const lua = new Lua(); | ||
const table = await lua.doString("x = {}; x.self = x; return x"); | ||
console.log(table instanceof LuaTable); | ||
const lua = new Lua(); | ||
console.log(await lua.doString("x = 1")); // null | ||
console.log(await lua.doString("return x")); // 1 | ||
try { | ||
await lua.doString("this is a syntax error"); // Error! | ||
} catch (e) { | ||
console.log(e); | ||
} | ||
console.log(await lua.doString("return 1 + 2")); // 3 | ||
console.log(await lua.doString(`return "abc"`)); // "abc" | ||
const table = await lua.doString(`return {key = "value", foo = "bar"}`); | ||
if (table instanceof LuaTable) { | ||
console.log("foo is: " + table.get("foo")); | ||
table.forEach((key, val) => { | ||
console.log(`key: ${key}, val: ${val}`); | ||
console.log(val instanceof LuaTable); | ||
console.log("table equalness", table === val); | ||
val.forEach((key, val) => { | ||
console.log(`key: ${key}, val: ${val}`); | ||
}); | ||
console.log(`key: ${key} val: ${val}`); | ||
}); | ||
const nil = await lua.doString("nil"); | ||
console.log('nil:', nil); | ||
console.log("Set foo = baz"); | ||
table.set("foo", "baz"); | ||
table.forEach((key, val) => { | ||
console.log(`key: ${key} val: ${val}`); | ||
}); | ||
lua.close(); | ||
})(); | ||
console.log("Set foo = {a = b}") | ||
const a = lua.newTable(); | ||
a.set("a", "b"); | ||
table.set("foo", a); | ||
table.forEach((key, val) => { | ||
console.log(`key: ${key} val: ${val}`); | ||
}); | ||
} | ||
const selfReference = await lua.doString("x = {} \n x.self = x \n return x"); | ||
if (selfReference instanceof LuaTable) { | ||
console.log("self is: " + selfReference.get("self")); | ||
selfReference.forEach((key, val) => { | ||
console.log(`key: ${key} val: ${val}`); | ||
if (val?.toString() === selfReference.toString()) { | ||
console.log("we have a cycle!"); | ||
} | ||
}); | ||
} | ||
lua.close(); | ||
})() |
@@ -6,3 +6,6 @@ import { promisify } from "util"; | ||
const binding_path = find(resolve(join(__dirname,'../package.json'))); | ||
const { luaNew, luaClose, luaDoString, luaTableForEach, luaTableToString } = require(binding_path); | ||
const { | ||
luaNew, luaClose, luaDoString, luaNewTable, | ||
luaTableGet, luaTableSet, luaTableForEach, luaTableToString, | ||
} = require(binding_path); | ||
@@ -13,3 +16,4 @@ const luaDoStringAsync = promisify(luaDoString); | ||
function convertLua(val: any): LuaValue { | ||
// Handles checking output type of lua value and wraps tables in a class. | ||
function wrapLua(val: any): LuaValue { | ||
if (val == null) { | ||
@@ -27,2 +31,15 @@ return null; | ||
// Unwraps classes before sending lua values back to the Lua context. | ||
function unwrapLua(val: LuaValue): any { | ||
if (val == null) { | ||
return null; | ||
} | ||
if (val instanceof LuaTable) { | ||
return val.table; | ||
} | ||
return val; | ||
} | ||
export class Lua { | ||
@@ -33,8 +50,12 @@ private lua: any; | ||
this.lua = luaNew(); | ||
} | ||
} | ||
async doString(code: string): Promise<LuaValue> { | ||
return convertLua(await luaDoStringAsync.call(this.lua, code)); | ||
return wrapLua(await luaDoStringAsync.call(this.lua, code)); | ||
} | ||
newTable(): LuaTable { | ||
return new LuaTable(luaNewTable.call(this.lua)); | ||
} | ||
close() { | ||
@@ -48,9 +69,17 @@ luaClose.call(this.lua); | ||
constructor(table: any) { | ||
constructor(table: null | any) { | ||
this.table = table; | ||
} | ||
get(key: LuaValue): LuaValue { | ||
return wrapLua(luaTableGet.call(this.table, unwrapLua(key))); | ||
} | ||
set(key: LuaValue, value: LuaValue): LuaValue { | ||
return wrapLua(luaTableSet.call(this.table, unwrapLua(key), unwrapLua(value))); | ||
} | ||
forEach(callback: (key: LuaValue, value: LuaValue) => void) { | ||
luaTableForEach.call(this.table, (key: any, value: any) => { | ||
callback(convertLua(key), convertLua(value)); | ||
callback(wrapLua(key), wrapLua(value)); | ||
}); | ||
@@ -57,0 +86,0 @@ } |
{ | ||
"name": "@cbrewster/luna", | ||
"version": "0.0.9", | ||
"version": "0.0.10", | ||
"description": "Node.js bindings to Lua", | ||
@@ -5,0 +5,0 @@ "license": "MIT", |
@@ -6,9 +6,39 @@ # @cbrewster/luna | ||
```javascript | ||
const { Lua } = require('@cbrewster/luna'); | ||
import { Lua, LuaTable } from '@cbrewster/luna'; | ||
(async () => { | ||
const lua = new Lua(); | ||
const result = await lua.doString("return 1 + 2"); | ||
console.log(result); // 3 | ||
const lua = new Lua(); | ||
console.log(await lua.doString("x = 1")); // null | ||
console.log(await lua.doString("return x")); // 1 | ||
try { | ||
await lua.doString("this is a syntax error"); // Error! | ||
} catch (e) { | ||
console.log(e); | ||
} | ||
console.log(await lua.doString("return 1 + 2")); // 3 | ||
console.log(await lua.doString(`return "abc"`)); // "abc" | ||
const table = await lua.doString(`return {key = "value", foo = "bar"}`); | ||
if (table instanceof LuaTable) { | ||
table.forEach((key, val) => { | ||
console.log(`key: ${key} val: ${val}`); | ||
}); | ||
} | ||
const selfReference = await lua.doString("x = {} \n x.self = x \n return x"); | ||
if (selfReference instanceof LuaTable) { | ||
selfReference.forEach((key, val) => { | ||
console.log(`key: ${key} val: ${val}`); | ||
if (val?.toString() === selfReference.toString()) { | ||
console.log("we have a cycle!"); | ||
console.log("Table: ", selfReference.toString()); | ||
} | ||
}); | ||
} | ||
lua.close(); | ||
})() | ||
``` |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Native code
Supply chain riskContains native code (e.g., compiled binaries or shared libraries). Including native code can obscure malicious behavior.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Native code
Supply chain riskContains native code (e.g., compiled binaries or shared libraries). Including native code can obscure malicious behavior.
Found 1 instance in 1 package
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
1256542
19
367
44
1