v8 provide the global object with Context::Global method, you should
wrap the document object first, and add it to the global object in the
entered context.

to wrap the document object, you must implement the named, indexed
property and call as function handler, and create the object with
ObjectTemplate

  v8::HandleScope handle_scope;

  v8::Handle<v8::ObjectTemplate> clazz = v8::ObjectTemplate::New();

  clazz->SetNamedPropertyHandler(NamedGetter, NamedSetter, NamedQuery,
NamedDeleter, NamedEnumerator);
  clazz->SetIndexedPropertyHandler(IndexedGetter, IndexedSetter,
IndexedQuery, IndexedDeleter, IndexedEnumerator);
  clazz->SetCallAsFunctionHandler(Caller);

  return v8::Persistent<v8::ObjectTemplate>::New(clazz);

if you could use python, it will like following code in pyv8 <http://
code.google.com/p/pyv8/>

from PyV8 import *

class Document(JSClass):
    name = "flier"

    def hello(self, s):
        print "hello " + s

class Global(JSClass):
    document = Document()

with JSContext(Global()) as ctxt:
    ctxt.eval("document.hello(document.name)")


On 1月22日, 下午4时15分, Andrey Semashev <[email protected]> wrote:
> Hi,
>
> I'm trying to port my application (a VoiceXML interpreter) from
> Mozilla SpiderMonkey to v8. For ones not familiar with VoiceXML, the
> application allows to run JS scripts in one of the several nested
> scopes, which are represented as objects. The scopes are session,
> application, document and dialog, in order from outer to inner scopes.
> Now, if a script is run in document scope, it can access variables and
> functions from outer scopes unqualified, and all variables and
> functions it defines are created as subobjects of the document scope.
> When the scope is left, all its subobjects are deleted.
>
> In SpiderMonkey, I can specify parent objects on their creation, so I
> can create scope objects hierarchy with the API calls. Also, I can
> specify an object, on which to execute the script, which allows to
> execute it in the given scope. How can I achieve similar functionality
> in v8?
>
> A few words on what I have tried so far. I managed to construct the
> scopes hierarchy by creating properties in the Object interface.
> However, I could not figure out the way to specify these objects for
> Script to execute on. I tried to wrap scripts in a "with" construct,
> but although it solves the scopes visibility problem, it doesn't allow
> to create variables in the current scope. For example, if I run this
> script:
>
>   var a = new Object();
>   function foo() { return "hello"; }
>
> in the document scope, I want document.a and document.foo objects to
> be created. Wrapping it in a "with (document)" block doesn't give
> that.
>
> I'm sorry if I'm missing something obvious, I'm relatively new to v8
> and JavaScript in general. Any suggestions would be welcome.

-- 
v8-users mailing list
[email protected]
http://groups.google.com/group/v8-users

Reply via email to