gamesys / moonshine

A lightweight Lua VM for the browser
http://moonshinejs.org
MIT License
501 stars 35 forks source link

How to convert LUA Table to javascript object #49

Open stackola opened 5 years ago

stackola commented 5 years ago

My question is basically in the title.

Is there a way to convert a table passed in from LUA to native JS?

{key:value} objects seem to work alright, but [value, value, value] arrays do not.

Here's an example:

Lua:

{obj = {key = "value"},  str = "string", arr = {1,2,3} }

Calling an environment function..

JS:

{
  "obj": {
    "__shine": {
      "type": "table",
      "index": 55,
      "keys": [],
      "values": [],
      "numValues": [
        null
      ],
      "refCount": 1
    },
    "key": "value"
  },
  "str": "string",
  "arr": {
    "__shine": {
      "type": "table",
      "index": 56,
      "keys": [],
      "values": [],
      "numValues": [
        null,
        1,
        2,
        3
      ],
      "refCount": 2
    }
  }
}

What I wanted:

{
  "obj": {
    "key": "value"
  },
  "str": "string",
  "arr": [
    1,
    2,
    3
  ]
}

This would obviously need to be done in a recursive manner.

Does a functionality like this exist already?