Data commit
This commit is contained in:
parent
7387c8f97b
commit
cb5bb5e222
199093 changed files with 3378972 additions and 0 deletions
41
Task/Object-serialization/Neko/object-serialization.neko
Normal file
41
Task/Object-serialization/Neko/object-serialization.neko
Normal file
|
|
@ -0,0 +1,41 @@
|
|||
/* Object serialization, in Neko */
|
||||
|
||||
var file_open = $loader.loadprim("std@file_open", 2)
|
||||
var file_write = $loader.loadprim("std@file_write", 4)
|
||||
var file_read = $loader.loadprim("std@file_read", 4)
|
||||
var file_close = $loader.loadprim("std@file_close", 1)
|
||||
|
||||
var serialize = $loader.loadprim("std@serialize", 1)
|
||||
var unserialize = $loader.loadprim("std@unserialize", 2)
|
||||
|
||||
/* Inheritance by prototype */
|
||||
proto = $new(null)
|
||||
proto.print = function () { $print(this, "\n") }
|
||||
|
||||
obj = $new(null)
|
||||
obj.msg = "Hello"
|
||||
obj.dest = $array("Town", "Country", "World")
|
||||
|
||||
$objsetproto(obj, proto)
|
||||
$print("Original:\n")
|
||||
obj.print()
|
||||
|
||||
/* Serialize the object */
|
||||
var thing = serialize(obj)
|
||||
var len = $ssize(thing)
|
||||
|
||||
/* To disk */
|
||||
var f = file_open("object-serialization.bin", "w")
|
||||
file_write(f, thing, 0, len)
|
||||
file_close(f)
|
||||
|
||||
/* Load the binary data into a new string space */
|
||||
f = file_open("object-serialization.bin", "r")
|
||||
var buff = $smake(len)
|
||||
file_read(f, buff, 0, len)
|
||||
file_close(f)
|
||||
|
||||
/* Unserialize the object into a new variable */
|
||||
var other = unserialize(buff, $loader)
|
||||
$print("deserialized:\n")
|
||||
other.print()
|
||||
Loading…
Add table
Add a link
Reference in a new issue