Awesome questions!
My understanding that in the global context (e.g. outside a function), there is
a difference between:
var f = 'x';
and
f = 'x';
Consider that the JavaScript is basically executing as if:
with (global) {
...
}
So var defines a variable, and without var defines a property of the global
object. As well, global variables defined with var are marked "DontDelete"
mschwartz@bytor:~/src/tmp$ cat test.js
include('lib/require.js');
console = require('builtin/console');
include('lib.js');
function main() {
f();
console.log(c + ' defined?');
}
mschwartz@bytor:~/src/tmp$ cat lib.js
var f = function() {
console.log('f()');
}
var c = "some variable";
mschwartz@bytor:~/src/tmp$ /usr/local/bin/silkjs ./test.js
f()
some variable defined?
As far as functions, they are marked "DontDelete" if defined:
function x() { ... }
vs.
x = function() { ... }
Read further...
On Feb 5, 2012, at 2:36 PM, Cédric Tabin wrote:
> Hi Michael,
>
> Thanks for your reply. I had a look into SilkJS and learned many useful
> things about V8 !
> After a deeper look, it seems that I found strange (maybe normal) behaviours
> when running scripts in the V8 engine. Let's take for example:
>
> == lib.js ==
> var f = function() { /* ... */ }
>
> == script.js ==
> include("lib.js");
> f();
>
> In that case, if you run script.js with SilkJS, all goes fine. Let's go
> further:
>
> == lib.js ==
> var f = function() { /* ... */ }
> var c = "some variable";
>
> == script.js ==
> include("lib.js");
> f();
> var x = c+" with some value"
>
> Here, when one want to run script.js, the engine cries that the variable 'c'
> is not defined (but 'f' still is) ! That sounds weird for me => in that case,
> shouldn't 'f' also be undefined ?
mschwartz@bytor:~/src/tmp$ cat test.js
include('lib/require.js');
console = require('builtin/console');
include('lib.js');
function main() {
f();
console.log(c + ' defined?');
}
mschwartz@bytor:~/src/tmp$ cat lib.js
var f = function() {
console.log('f()');
}
var c = "some variable";
mschwartz@bytor:~/src/tmp$ /usr/local/bin/silkjs ./test.js
f()
some variable defined?
> Now as another weird behaviour, let's take a look at some function overriding:
>
> == lib.js ==
> var f = function() { return "Library"; }
>
> == script.js ==
> include("lib.js");
> var f = function() { return "Script"; }
> f(); //yields 'Script'
>
> Ok, the overriding looks fine in that case.
>
mschwartz@bytor:~/src/tmp$ cat test2.js
include('lib/require.js');
console = require('builtin/console');
include('lib2.js');
var f = function() {
return "overridden f()";
}
function main() {
console.log(f());
}
mschwartz@bytor:~/src/tmp$ cat lib2.js
var f = function() {
return 'original f()';
}
mschwartz@bytor:~/src/tmp$ /usr/local/bin/silkjs ./test2.js
overridden f()
> == lib.js ==
> function f() { return "Library"; }
>
> == script.js ==
> include("lib.js");
> function f() { return "Script"; }
> f(); //yields 'Library'
>
mschwartz@bytor:~/src/tmp$ cat test3.js
include('lib/require.js');
console = require('builtin/console');
include('lib3.js');
function f() {
return "overridden f()";
}
function main() {
console.log(f());
}
mschwartz@bytor:~/src/tmp$ cat lib3.js
function f() {
return 'original f()';
}
mschwartz@bytor:~/src/tmp$ /usr/local/bin/silkjs ./test3.js
original f()
> And here the overriding doesn't work... It seems that semantically, it is
> exactly the same, so what's the change behind ? Probably I also missed some
> point here :-) Hopefully, some of you can hellp me understand what's going
> on...
>
> Best regards,
> Cédric
>
> On Thu, Feb 2, 2012 at 15:38, Michael Schwartz <[email protected]> wrote:
> Take a look at SilkJS. It wraps v8 in C++, and provides both include() and
> require() methods. You're describing require(), which only loads the library
> once, but returns the module or namespace for it as many times as you
> import/require it.
>
> http://github.com/mschwartz/SilkJS
>
> And commons.org for docs on require()
>
> Sent from my iPhone
>
> On Feb 1, 2012, at 2:50 PM, Cédric Tabin <[email protected]> wrote:
>
> > Hello,
> >
> > I'm a new user of the V8 framework and try to play with it :-)
> > Currently, I'd like to handle a library (in javascript) that will be
> > used by other scripts, all run by the V8's engine. For example I will
> > have something like that:
> >
> > == library.js ==
> > function doSomething() { print "library"; }
> >
> > == script1.js ==
> > /* import/include library.js */
> > function doSomething() { print "script1"; } //override the doSomething
> > method from library.js
> > doSomething(); //should return *script1*
> >
> > == script2.js ==
> > /* import/include library.js */
> > doSomething(); //should return *library*
> >
> >
> > My goal is to avoid reexecuting the whole file library.js before
> > running script1.js or script2.js. In my C++ code, I'd like to do
> > something like that:
> > a) prepare environment (Context, Scope, ...)
> > b) compile & execute library.js
> > c) take a "snapshot" of the current state (or something like that)
> > d) compile & execute script1.js
> > e) reset the state in (c)
> > f) compile & execute script2.js (so it runs like if script1.js was
> > never ran, withou re-running library.js)
> >
> > I had a look to the Contexts, but it is to run for independant code
> > which is clearly not my case... I also tried to play with Scope and
> > HandleScope with no result.
> >
> > Is there a way to handle this problem ?
> >
> > Thanks & best regards,
> > Cedric
> >
> > --
> > v8-users mailing list
> > [email protected]
> > http://groups.google.com/group/v8-users
>
> --
> v8-users mailing list
> [email protected]
> http://groups.google.com/group/v8-users
>
>
> --
> v8-users mailing list
> [email protected]
> http://groups.google.com/group/v8-users
--
v8-users mailing list
[email protected]
http://groups.google.com/group/v8-users