Fengari is a lua VM written in Javascript. It's implementation makes use of the JS garbage collector, which means it is fully capable of cross language interop.
- Call any JS function from Lua
- Give Lua tables/functions/userdata to Javascript
js = require "js"
A userdata representing JavaScript null
A reference to the JavaScript global context. In the browser, this is usually equivalent to the window
object. In node.js it's equal to global
.
Invokes the JavaScript new
operator on constructor
passing the arguments specified.
Returns the created object.
Returns a iterating function and an iterator state that behave like a JavaScript for...of loop. Suitable for use as a lua iterator. e.g.
for f in js.of(js.global:Array(10,20,30)) do
print(f)
end
Creates a JavaScript Proxy object. The proxy supports calling (apply
), indexing (get
and has
) and setting (set
and deleteProperty
).
type
is the desired result for typeof proxy
; it may be "function"
(the default) or "object"
.
Note that JavaScript coerces all types except Symbols to strings before using them as a key in an indexing operation.
Coerces the value x
to a number using JavaScript coercion rules.
Returns if the value x
is an instance of the class y
via use of the JavaScript instanceof
operator
The __pairs
Symbol can be used to describe how to iterate over a JavaScript object. Use Symbol.for("__pairs")
to get the symbol. It should be used as a key on your objects, where the value is a function returning an object with three properties: "iter"
, "state"
and "first"
.
"iter"
should be a function that follows the standard Lua generic for protocol, that is, it gets called with your state (as this
) and the previous value produced; it should return an array of values or undefined
if done.
e.g. to make pairs
on a Map return entries in the map via the iterator symbol
Map.prototype[Symbol.for("__pairs")] = function() {
return {
iter: function(last) {
var v = this.next();
if (v.done) return;
return v.value;
},
state: this[Symbol.iterator]()
};
}
A default __pairs
is attached to Object.prototype
that uses Object.keys
.