Number.parseInt.toString()
> "function parseInt() { [native code] }"
class Script: public Struct {
public:
// Script types.
enum Type {
TYPE_NATIVE = 0,
TYPE_EXTENSION = 1,
TYPE_NORMAL = 2,
TYPE_WASM = 3,
TYPE_INSPECTOR = 4
};
...
Handle<String> JSFunction::ToString(Handle<JSFunction> function) {
Isolate* const isolate = function->GetIsolate();
Handle<SharedFunctionInfo> shared_info(function->shared(), isolate);
// Check if {function} should hide its source code.
if (!shared_info->IsUserJavaScript()) {
return NativeCodeFunctionSourceString(shared_info);
}
...
v8::Extension(kLoadTimesExtensionName,
"var chrome;"
"if (!chrome)"
" chrome = {};"
"chrome.loadTimes = function() {"
" native function GetLoadTimes();"
" return GetLoadTimes();"
"};"
"chrome.csi = function() {"
" native function GetCSI();"
" return GetCSI();"
"}") {}
...
chrome.date = function() {
return Date();
};
readonly attribute unsigned long long firstLayout;
ScriptValue PerformanceTiming::toJSONForBinding
(ScriptState* scriptState) const
{
...
result.addNumber("firstLayout", firstLayout());
return result.scriptValue();
}
Chromium + Node
脚本引擎扩展 | 渲染引擎 | V8 API扩展 | 其他宿主 | |
---|---|---|---|---|
Pros | 开发成本低 结构简单 可访问内核资源多 |
IDL 规范 接口标准 可访问内核资源多 |
开发成本低 可访问内核资源多 |
功能强大 扩展性 学习成本低 |
Cons | 接口类型简单 | build 时间长 直接修改内核 |
直接修改内核 | 开发、维护成本高 结构复杂 |