Victor jiang
Victor jiang

Reputation: 33

How to use V8's built in functions

I'm new in both javascript and V8. According to Google's Embedder's Guide, I saw something in the context section talking about built-in utility javascript functions. And I also found some .js files(e.g. math.js) in the downloaded source code, so I tried to write a simple program to call functions in these files, but I failed.

  1. Does a context created by Persistent<Context> context = Context::New() have any built-in js functions? How can I access them?

  2. Is there a way to first import existing js files as a library(something like src="xxx" type="text/javascript" in HTML page) and then run my own execute script?

  3. Can I call google maps api through the embedded V8 library in app? How?

Upvotes: 3

Views: 2438

Answers (3)

Rames
Rames

Reputation: 48

You can use for example the --allow_natives_syntax or --expose_natives_as option.
Here are examples with MathLog picked at random in src/math.js:

First compile a shell with

$ scons d8 -j8

Then use --expose_natives_as:

$ ./d8 --expose_natives_as nat
V8 version 3.12.7 (candidate) [console: dumb]
d8> nat.MathLog(100)
4.605170185988092

or use --allow_natives_syntax with the '%' prefix:

$ ./d8 --allow_natives_syntax
V8 version 3.12.7 (candidate) [console: dumb]
d8> %MathLog(100)
4.605170185988092

Upvotes: 0

HFLW
HFLW

Reputation: 434

I think v8 gives you the Math.* functions for free.

You need to implement everything else yourself though, like loading other javascript files. shell.cc has some of the functions you might be looking for.

As for the maps API, I believe you would need a full blown rendering engine/javascript engine combo for that. You might be better off taking a look at Webkit or something that you can use to embed Webkit for what you're looking to do, I can't really say.

Upvotes: 0

Nicol&#225;s
Nicol&#225;s

Reputation: 7533

3. Google Maps needs a full browser DOM (or at least XMLHttpRequest I guess), you can't use it from just a Javascript library.

Upvotes: 1

Related Questions