Can you populate a v8::Array from C++?

3,304 views
Skip to first unread message

Brett C.

unread,
Sep 2, 2008, 5:54:16 PM9/2/08
to v8-users
I was looking at samples/shell.cc and noticed that it is missing any
way to get command-line arguments (e.g. Rhino provides an arguments
array that leaves off the executable). I thought I could quickly
modify shell.cc, but when I looked at the public API I noticed that
all v8::Array provides as a public API is New(), Cast() and Length().
There does not seem to be any direct way to manipulate the array to
add values.

What would be the best way to go about setting argv to an 'arguments'
array as a global variable? Or is the best alternative a function that
takes an index and returns the string for a specific index? In the
latter case I would then like to know how to return null or undefined.

-Brett

Feng Qian

unread,
Sep 2, 2008, 6:40:20 PM9/2/08
to v8-u...@googlegroups.com
Hi Brett,

I think the only way to do it is to create a JS Array object in shell.cc, and for each C argument, create a JS string object, then set to the array object using the same index. The last step is to set the array object as a property named 'arguments' to the global object of the context.

Here is a draft of the code look like:
v8::Local<v8::Array> arguments = v8::Array::New(argv);
for (int i = 0; i < args; i++) {
  arguments.Set(v8::Number::New(i), v8::String::New(args[i]));
}
context->Global()->Set(v8::String::New("arguments"), arguments);


Then the JavaScript can access the 'arguments' global variable in the global scope.

Cheers,
Feng

Matt

unread,
Sep 13, 2008, 9:22:18 AM9/13/08
to v8-users
v8r -- the javascript interpreter using v8 I'm working on does this...
http://code.google.com/p/v8r/source/browse/trunk/samples/v8r.cc

-- Start by defining something like --

int xARGC = 0;
v8::Local<v8::Array> xARGV;
v8::Handle<v8::Value> ExternalGetArgv(const v8::Arguments& args) {
v8::Context::Scope context_scope(gblContext);
v8::HandleScope handle_scope;
return xARGV;
}
v8::Handle<v8::Value> ExternalGetArgc(const v8::Arguments& args) {
v8::Context::Scope context_scope(gblContext);
v8::HandleScope handle_scope;
return v8::Integer::New(xARGC);
}

-- Then to get the args in the array (this is inside the main()) --

xARGC = argc - 2;
if ( xARGC > 0 ) {
xARGV = v8::Array::New(argc);
int x = 0;
for(int i = 2; i <= argc; i++) {
if ( NULL != argv[i] ) {
v8::Handle<v8::Number> y = v8::Number::New( x );
v8::Handle<v8::String> s = v8::String::New( argv[i] );
xARGV->Set( y, s);
x++;
}
}
}

This example is used in v8r and chops off the first two args becuase
they contain v8r and the script being executed.

Enjoy,
Matt

On Sep 2, 5:54 pm, "Brett C." <bcan...@gmail.com> wrote:
> I was looking at samples/shell.cc and noticed that it is missing any
> way to get command-line arguments (e.g. Rhino provides an argumentsarraythat leaves off the executable). I thought I could quickly
> modify shell.cc, but when I looked at the public API I noticed that
> all v8::Arrayprovides as a public API is New(), Cast() and Length().
> There does not seem to be any direct way to manipulate thearrayto
> add values.
>
> What would be the best way to go about setting argv to an 'arguments'arrayas a global variable? Or is the best alternative a function that
Reply all
Reply to author
Forward
0 new messages