register callback for marshal in VM (#19578)
* register callback for marshal in VM * remove unrelated code * polish * more tests * more tests * add loadVM and toVM
This commit is contained in:
parent
6454472938
commit
8f9dd5b349
6 changed files with 96 additions and 59 deletions
|
|
@ -32,7 +32,7 @@ from system/formatfloat import addFloatRoundtrip, addFloatSprintf
|
|||
|
||||
|
||||
# There are some useful procs in vmconv.
|
||||
import vmconv
|
||||
import vmconv, vmmarshal
|
||||
|
||||
template mathop(op) {.dirty.} =
|
||||
registerCallback(c, "stdlib.math." & astToStr(op), `op Wrapper`)
|
||||
|
|
@ -152,6 +152,7 @@ when defined(nimHasInvariant):
|
|||
proc stackTrace2(c: PCtx, msg: string, n: PNode) =
|
||||
stackTrace(c, PStackFrame(prc: c.prc.sym, comesFrom: 0, next: nil), c.exceptionInstr, msg, n.info)
|
||||
|
||||
|
||||
proc registerAdditionalOps*(c: PCtx) =
|
||||
|
||||
template wrapIterator(fqname: string, iter: untyped) =
|
||||
|
|
@ -344,3 +345,36 @@ proc registerAdditionalOps*(c: PCtx) =
|
|||
addFloatSprintf(p.strVal, x)
|
||||
|
||||
wrapIterator("stdlib.os.envPairsImplSeq"): envPairs()
|
||||
|
||||
registerCallback c, "stdlib.marshal.toVM", proc(a: VmArgs) =
|
||||
let typ = a.getNode(0).typ
|
||||
case typ.kind
|
||||
of tyInt..tyInt64, tyUInt..tyUInt64:
|
||||
setResult(a, loadAny(a.getString(1), typ, c.cache, c.config, c.idgen).intVal)
|
||||
of tyFloat..tyFloat128:
|
||||
setResult(a, loadAny(a.getString(1), typ, c.cache, c.config, c.idgen).floatVal)
|
||||
else:
|
||||
setResult(a, loadAny(a.getString(1), typ, c.cache, c.config, c.idgen))
|
||||
|
||||
registerCallback c, "stdlib.marshal.loadVM", proc(a: VmArgs) =
|
||||
let typ = a.getNode(0).typ
|
||||
let p = a.getReg(1)
|
||||
var res: string
|
||||
|
||||
var node: PNode
|
||||
case p.kind
|
||||
of rkNone:
|
||||
node = newNode(nkEmpty)
|
||||
of rkInt:
|
||||
node = newIntNode(nkIntLit, p.intVal)
|
||||
of rkFloat:
|
||||
node = newFloatNode(nkFloatLit, p.floatVal)
|
||||
of rkNode:
|
||||
node = p.node
|
||||
of rkRegisterAddr:
|
||||
node = p.regAddr.node
|
||||
of rkNodeAddr:
|
||||
node = p.nodeAddr[]
|
||||
|
||||
storeAny(res, typ, node, c.config)
|
||||
setResult(a, res)
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue