-
Notifications
You must be signed in to change notification settings - Fork 587
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Is there a way serialize *otto.Otto and save it to disk? #133
Comments
I feel like there are two "levels" for this kind of feature.
|
Agreed. Some kind of global state serialisation based on clone seems a good area to investigate and could be useful. I think point 2 might be doable if you make some assumptions:
If all the assumptions there are true, I would guess you could have a special serialisation format for native functions that annotates the full package path, object references, etc to be able to find and replumb in the function call. However, thats not trivial by any stretch of the imagination. I'm not entirely convinced of the utility of this though, it seems like a lot of work for some very limited use-cases. Arguably it would be better to write a book-keeping wrapper around your runtime that tracks native functions being attached and has it's own serialisation system that works in tandem with the stuff in point 1 to rebuild your execution context, as typically the functiosn you want to expose are going to be from all over the place and otto can't be expected to have the required domain knowledge to work in a generic sense. |
I use a simple-ish way to export and save most of what I actually need. var $exclude = "$exclude,console,..."// replace ... with other functions to ignore
$exclude = $exclude.split(",")
function exportGlobal(src) {
var dst = {}
var keys = Object.keys(src)
for (var i = 0; i < keys.length; i++) {
var key = keys[i]
// skip exluded keys
if ($exclude.indexOf(key) != -1) {
continue;
}
var element = src[key];
if (typeof element == "function") {
dst[key] = "func://" + element.toString()
} else {
dst[key] = element
}
}
return JSON.stringify(dst)
}
function importGlobal(dst, src) {
var keys = Object.keys(src)
for (var i = 0; i < keys.length; i++) {
var key = keys[i]
// skip exluded keys
if ($exclude.indexOf(key) != -1) {
continue;
}
var element = src[key];
if (typeof element == "string" && element.substring(0, 7) == "func://") {
eval("dst[key] = " + element.substring(7))
} else {
dst[key] = element
}
}
} And I run on save: o.Run("exportGlobal(this)") On load: o.Run("importGlobal(this, " + old + ")") It saves global functions but not nested ones. |
Hi!
Is there a way to serialize the Otto-object (or some other context-containing-object)?
I would like to save javascripts
this
(with all dynamicly added fields) to disk and be able to load it after a restart.Simply pushing it into a gob.Encoder (or json.Encoder) seems not to work :-)
The text was updated successfully, but these errors were encountered: