Skip to content

Commit

Permalink
0.0.10
Browse files Browse the repository at this point in the history
  • Loading branch information
disruptek committed Mar 8, 2023
1 parent 0398e8c commit b7b78f3
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 7 deletions.
10 changes: 4 additions & 6 deletions lunacy.nim
Original file line number Diff line number Diff line change
Expand Up @@ -630,7 +630,8 @@ proc toLuaValue*[K, V](a: TableLike[K, V]): LuaValue =
for key, value in a.pairs:
result[key.toLuaValue] = value.toLuaValue

proc push*(L: PState; value: LuaValue): LuaStack =
proc push*(L: PState; value: LuaValue) =
## push a LuaValue onto the stack
case value.kind
of TBoolean:
L.pushBoolean value.truthy.cint
Expand All @@ -645,11 +646,8 @@ proc push*(L: PState; value: LuaValue): LuaStack =
# FIXME: exploit createTable()?
L.newTable
for key, value in value.table.pairs:
discard L.push key
discard L.push value
L.push key
L.push value
L.setTable -3
else:
raise LuaError.newException "not implemented"
let pos = L.last
assert pos.readValidType == value.kind
result = newLuaStack(value.kind, pos)
2 changes: 1 addition & 1 deletion lunacy.nimble
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
version = "0.0.9"
version = "0.0.10"
author = "disruptek"
description = "lua hacks"
license = "MIT"
Expand Down
55 changes: 55 additions & 0 deletions tests/test.nim
Original file line number Diff line number Diff line change
@@ -1,9 +1,22 @@
import std/json
import std/tables

import pkg/balls

import lunacy

proc read_it(L: PState; ud: pointer; size: ptr cint): cstring {.cdecl.} =
template s: string = cast[ptr string](ud)[]
size[] = s.len
result = s
#copyMem(addr result, ud, s.len)

proc write_it(L: PState; p: pointer; size: cint; ud: pointer): cint {.cdecl.} =
template s: string = cast[ptr string](ud)[]
let l = s.len
setLen(s, l + size)
copyMem(addr s[l], p, size)

proc main =
suite "lunacy":
block:
Expand Down Expand Up @@ -76,4 +89,46 @@ proc main =
check table["a".toLuaValue] == 1.toLuaValue
check table["b".toLuaValue] == 2.toLuaValue

block:
## compile a chunk, remove it from the vm,
## reinsert it, invoke it on different inputs
let vm = newState()
defer: close vm
let a: cstring = "a"
let b: cstring = "b"
vm.push(5.toLuaValue)
vm.setGlobal a
vm.push(6.toLuaValue)
vm.setGlobal b
let source: cstring = "return { a, b }"
doAssert 0 == vm.loadString(source)
doAssert vm.isFunction(-1)
var data: string
doAssert 0 == vm.dump(write_it, addr data)
vm.pop(1)
let js = newJString data
echo js
var hmm: string = getStr js
const fun: cstring = "fun"
doAssert 0 == vm.load(read_it, addr hmm, fun)
doAssert vm.isFunction(-1)
vm.setGlobal fun
vm.getGlobal fun
doAssert 0 == vm.pcall(0, MultRet, 0)
doAssert vm.isTable(-1)
var s: LuaStack
s = vm.popStack(expand=true)
echo "(1) ", s.value
vm.push(7.toLuaValue)
vm.setGlobal a
vm.push(9.toLuaValue)
vm.setGlobal b
vm.getGlobal fun
doAssert vm.isFunction(-1)
doAssert 0 == vm.pcall(0, MultRet, 0)
doAssert vm.isTable(-1)
s = vm.popStack(expand=true)
echo "(2) ", s.value
doAssert vm.isNil(-1)

main()

0 comments on commit b7b78f3

Please sign in to comment.