How to use the built-in functions of V8

I am new to javascript and V8. According to the Google Embedder Guide, I saw something in the section on javascript built-in functions. And I also found some .js files (for example, math.js) in the downloaded source code, so I tried to write a simple program to call functions in these files, but I failed.

  • Are there any Persistent<Context> context = Context::New()built-in js functions in the context created ? How can I access them?

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

  • Is it possible to use google maps api through the built-in V8 library in the application? How?

+3
source share
3 answers

3. Google Maps requires a full DOM browser (or at least XMLHttpRequest, I think), you cannot use it only from the JavaScript library.

+1
source

I think v8 gives you Math functions. * is free.

You need to implement everything else yourself, for example, load other javascript files. shell.cc contains some features you can look for.

As for the map APIs, I believe that for this you will need a full-featured engine engine / javascript compiler. Perhaps you should take a look at Webkit or something that you can use to embed Webkit for what you want to do, I cannot say.

0

You can use, for example, the --allow_natives_syntax or --expose_natives_as option.
Here are examples where MathLog is randomly selected in src / math.js:

First compile the 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
0
source

Source: https://habr.com/ru/post/1736599/


All Articles