三、V8 引擎的使用,使用 d8

162 阅读1分钟

编译好 google v8 后,使用 d8

tools/dev/gm.py x64.debug # 编译好v8以后,进入out/x64.debug目录下

image.png

./d8 # 执行d8

d8>"hello world";
"hello world"
d8>1
1
d8>1+2;
3
d8>typeof "abc";
"string"
d8>typeof 1;
"number"
d8>function add(x, y) { return x + y; }; add(1,2);
3
d8>typeof JSON;
"object"
d8>new Map();
[object Map]
d8>JSON;
[object JSON]
d8> Map;
function Map() { [native code] }
d8> Set;
function Set() { [native code] }
d8> Promise;
function Promise() { [native code] }
d8> JSON.parse;
function parse() { [native code] }
d8> JSON.parse("{}");
{}
d8> Math;
[object Math]
d8> Math.random();  
0.4119332573900518
d8> var setObj = new Set(); setObj.add("1"); setObj.has("1");     
true
d8> console
[object console]
# nodejs 的模块是没有的。
d8> OS;
(d8):1: ReferenceError: OS is not defined
OS;
^
ReferenceError: OS is not defined
    at (d8):1:1

d8> Path;
(d8):1: ReferenceError: Path is not defined
Path;
^
ReferenceError: Path is not defined
    at (d8):1:1

例子一

// test.js
new Promise((resolve, reject) => {
  setTimeout(function () {
    resolve(1);
  }, 1000);
})
./d8 test.js #没有任何输出

例子二

// test1.js
new Promise((resolve, reject) => {
  setTimeout(function () {
    resolve(1);
  }, 1000);
}).then(res => {
  console.log("输出:", res);
})
$>./d8 test1.js
$>输出: 1

例子三

// test2.js
class Custom {
  static props = {
    a: 1
  }
  b = 1;
  toString() {
    console.log("Custom Class");
  }
}

const custom = new Custom();

custom.toString();

console.log(Custom.props.a);
console.log(custom.b);

$>Custom Class
$>1
$>2