Skip to content

Commit

Permalink
Merge pull request #10 from kiss-lang/getGlobalVar
Browse files Browse the repository at this point in the history
getGlobalVar return the var as haxe value (rebased)
  • Loading branch information
kevinresol authored Jul 25, 2024
2 parents 9168ead + 4b91cc5 commit 150ffd9
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 4 deletions.
7 changes: 5 additions & 2 deletions src/vm/lua/Lua.hx
Original file line number Diff line number Diff line change
Expand Up @@ -87,8 +87,11 @@ class Lua {
lua_setglobal(l, name);
}

public function getGlobalVar(name:String) {
return lua_getglobal(l, name);
public function getGlobalVar(name:String):Any {
lua_getglobal(l, name);
var v = toHaxeValue(l, -1);
lua_pop(l, -1);
return v;
}

public function destroy() {
Expand Down
12 changes: 10 additions & 2 deletions tests/RunTests.hx
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,14 @@ class RunTests {
asserts.assert(compare(Success(1), lua.tryRun('return foo.val()'))); // this make sure this-binding in js is working
return asserts.done();
}

public function get() {
vm.lua.Lua.badConversionBehavior = Warn;
lua.setGlobalVar('foo', new Foo());
var foo:{a:Int} = lua.getGlobalVar('foo');
asserts.assert(compare(1, foo.a));
return asserts.done();
}

public function func() {
function add(a:Int, b:Int) return a + b;
Expand Down Expand Up @@ -146,8 +154,8 @@ class RunTests {

@:keep
class Foo {
var a = 1;
var b = '2';
public var a = 1;
public var b = '2';
public function new() {}
public function add(a:Int, b:Int) return a + b;
public function val() return a;
Expand Down

0 comments on commit 150ffd9

Please sign in to comment.