exval
exval allows uneval
JavaScript
objects back to source code (including functions and classes!).
This allows making shallow copy of an object and recreate it on other
machine.
WARNING: This library is under development. Many features may not work, throws exceptions or works partly. If you find a bug, please open an issue or consider contributing to this project.
If you're interested in running JavaScript on a remote machine you should checkout remote-lib as well.
Examples
Object Cloning
const Exval = ; // require exvalconst exval = ; // create a new exval instance const obj = foo: 'bar' deep: hello: 'world' pow: Mathpow; const output = exval;console; // {foo:'bar',deep:{hello:'world'},pow:Math.pow} const obj2 = ;;;assert;
Class Instance
const Exval = ; // require exvalconst exval = ; // create a new exval instance // create a counter instanceconst c1 = { { thiscounter = init; } { thiscounter += a; } return 100;}; // notice that we didn't define `Counter` class hereassert; // update the counter and add some custom propertiesc1;c1foo = 'bar'; // generate the counter instance code and run itconst output = exval;const c2 = ; // Counter class is still undefined but our counter cloned successfullyassert;assert;assert; // we can even continue to use it as normal!c2;assert; // the original counter stay the sameassert;
Multiple References
const Exval = ; // require exvalconst exval = ; // create a new exval instance const a = name: 'a' ; const obj = a1: a a2: a; const output = exval;console; // function(){var a={name:'a'};return {a1:a,a2:a}}() const obj2 = ;assert;;
Close Loop References
const Exval = ; // require exvalconst exval = ; // create a new exval instance const obj = foo: 'bar' ;objobj = obj; const output = exval;const obj2 = ; ; ;;
Limitations
Global Vars
exvar will not copy outer scope variables that has been used in your code (including globals and environment variables). It's your responsibility to make sure that all the globals variables are correctly copied and transferred between your machines.
let glob = 1; { return glob++;} const output = exval; console; // prints 'function counter() {\nreturn glob++;\n}' // notice that the variable `glob` has been ommited
Closures
exvar can't access variables in your inner closure. Therefore it's your responsibility to regenerate them before you can use the generated code.
const inc = { let counter = 0; return counter++;}; console // "0"console // "1"console // "undefined" const output = exval;const inc2 = ; ; // Throws "ReferenceError: counter is not defined"
supper
Keyword
The It's impossible to know a method parent class outside the class context.
Therefore, calling methods that using the super
keyword will fail to
run, although exval
ing the hole class will works!
{ return 'Foo'; } { return `Bar`; } // create shallow copy of the hole class will workexval; // create shallow copy of the method `getName`// throws "SyntaxError: 'super' keyword unexpected here"exval;
.bind
Function Binding a function will hide it sourcecode so exvar couldn't access
the original function sourcecode. Please prefer to use arrow functions
instead of .bind
.
{ return a + 1; }const foo2 = ;const foo5 = foo; // exval'ing arrow function will workexval; // returns "() => foo(2)" // exval'ing bind function will not work// throws ReferenceError: Couldn't encode native code "function () { [native code] }"exval;
Symbols
Exval currently ignoring object symbols:
const kFoo = Symbol'foo';const obj = kFoo: 'Foo!'; exval; // returns "{}"
License
MIT License. Copyright © 2016 Moshe Simantov