Dennis,

> include("test.js");
> test.func1();

I'm working on something similar to the namespace-esque thing you're
talking about here.  However, rather than have the file create an
object based on the filename (which is blackbox and magic and spooky),
I plan to have it let you pass in an object that will be used as the
global scope for that code.

I'm doing this in k7.  Some of the parts are there, but there are some
odd edge case bugs and of course it's a work in progress.

The API would be something like this:

var code = system.posix.readfile("test.js");
var testobj = { foo : "bar", parent : this };
var result = system.engine.run(code, testobj);

However, there's a lot of fairly hairy stuff involved in tossing
global scopes around.  They're not like regular objects at all.  I had
done some research into how v8 handles them, but had to take a break
from it for a few weeks for the day job.

I have some of it done on http://github.com/isaacs/k7/tree/engine-module

Have fun!

--i


On Apr 7, 1:28 pm, Dennis H <[email protected]> wrote:
> Hi everyone!  I haven't been using v8 for very long, so please forgive
> me if I ask a stupid question... =(
>
> Anyway, I was playing around with v8, and I noticed that there wasn't
> a way to "import", so to speak, other Javascript files (within
> Javascript) into the main one that the C++ program compiles and runs.
> Searching online wasn't much of a help, since almost all of the ways I
> found involved web-specific techniques (eg. document.write(...)).
>
> I wrote a small function that _seems_ to work, but I'm not sure if I'm
> doing things correctly/efficiently...
>
> v8::Handle<v8::Value> Include(const v8::Arguments& args)
> {
> v8::HandleScope handle_scope;
> v8::Handle<v8::String> jsfile = ReadFile(*v8::String::Utf8Value(args
> [0]));  //ReadFile is from shell.cc
> v8::Handle<v8::Script> script = v8::Script::Compile(jsfile);
> script->Run();
> return v8::Undefined();
>
> }
>
> Is there a better way to import/include a Javascript file into
> another?
>
> And secondly, I was thinking about having the function return all of
> the file's functions, variables, etc., as an object, for a namespace-
> esque effect...
>
> eg.
> include("test.js");
> test.func1();
>
> ...but I'm not entirely sure how to go about doing that.
>
> Has anyone else dealt with this issue?  Any solutions would be welcome
> =).
>
> Cheers,
> ~Dennis
--~--~---------~--~----~------------~-------~--~----~
v8-users mailing list
[email protected]
http://groups.google.com/group/v8-users
-~----------~----~----~----~------~----~------~--~---

Reply via email to