ceifa / wasmoon

A real lua 5.4 VM with JS bindings made with webassembly
MIT License
449 stars 29 forks source link

Convert JavaScript object (`{}`) to Lua table #74

Closed TechStudent10 closed 1 year ago

TechStudent10 commented 1 year ago

Is there a way to convert a JavaScript object (that also contains other objects) to a Lua table?

When I attempt to do this myself by having a function that returns a JS object, Lua assigns it nil

image

ceifa commented 1 year ago

Yes, take a look on the readme for an example

TechStudent10 commented 1 year ago

Where? There is no example of this on the README.

ceifa commented 1 year ago

Where? There is no example of this on the README.

Not exactly converting a JS object to a table, but it's the same concept of the example on the readme:

lua.global.set('window', window) // Converting the engire window object to a lua table
const luaWindow = lua.global.get('window') // Convert the window lua table to a JS object
TechStudent10 commented 1 year ago

Where? There is no example of this on the README.

Not exactly converting a JS object to a table, but it's the same concept of the example on the readme:

lua.global.set('window', window) // Converting the engire window object to a lua table
const luaWindow = lua.global.get('window') // Convert the window lua table to a JS object

Ah sorry. I mean returning a JavaScript object in a Lua function.

I have this (Lua) code:

element = getElement("Text0")

getElement returns a JavaScript object in JavaScript, but I need it to be a Lua table.

ceifa commented 1 year ago

I don't understand exactly what you want. Can you make a minimal repro?

TechStudent10 commented 1 year ago

@ceifa (sorry if something is wrong I'm currently on my phone)

import { LuaFactory } from "wasmoon"

const factory = new LuaFactory()
const lua = factory.createEngine()

lua.global.set("getObject", () => {
    return {hello: "world"}
}

await lua.doString(`
obj = getObject()
print(obj)
`)

The output should be {"hello": "world"}, but it is instead nil.

ceifa commented 1 year ago

Tested the exactly same code and it works: https://codesandbox.io/s/cold-http-gxe4yi?file=/src/index.js

TechStudent10 commented 1 year ago

Oh gosh I'm dumb :/

I put the return statement in another set of parenthesis. Sorry for wasting your time on a dumb mistake :/

image