@@ -3660,6 +3660,72 @@ Cython 提供了一种机制(编译器)编译 Python 代码为【原生代
3660
3660
3661
3661
另外,它也提供了把 C/C++ 代码编译为 Python 模块的机制。
3662
3662
3663
+ === 17.2.2 整合 JavaScript 语言 ===
3664
+
3665
+ <h4>V8</h4>
3666
+
3667
+ Home:[https://github.com/v8/v8]
3668
+
3669
+ Links:[https://en.wikipedia.org/wiki/Chrome_V8 Wikipedia ]
3670
+
3671
+ 这就是大名鼎鼎的 V8 引擎,Google 公司开发,C++ 编写,被用于 Chrome 浏览器。
3672
+
3673
+ 既然用于 Chrome 浏览器,显然【完整地】支持了 JS 语言的规范(ECMA-262)。
3674
+
3675
+ 它既可以独立运行,也可以嵌入到 C++ 程序中。
3676
+
3677
+ 代码示例——Hello world
3678
+ <source lang="cpp">
3679
+ #include <stdio.h>
3680
+ #include <stdlib.h>
3681
+ #include <string.h>
3682
+ #include <libplatform/libplatform.h>
3683
+ #include <v8.h>
3684
+
3685
+ int main(int argc, char* argv[])
3686
+ {
3687
+ // Initialize V8.
3688
+ v8::V8::InitializeICUDefaultLocation(argv[0]);
3689
+ v8::V8::InitializeExternalStartupData(argv[0]);
3690
+ std::unique_ptr<v8::Platform> platform = v8::platform::NewDefaultPlatform();
3691
+ v8::V8::InitializePlatform(platform.get());
3692
+ v8::V8::Initialize();
3693
+
3694
+ // Create a new Isolate and make it the current one.
3695
+ v8::Isolate::CreateParams create_params;
3696
+ create_params.array_buffer_allocator = v8::ArrayBuffer::Allocator::NewDefaultAllocator();
3697
+ v8::Isolate* isolate = v8::Isolate::New(create_params);
3698
+
3699
+ {
3700
+ v8::Isolate::Scope isolate_scope(isolate);
3701
+ // Create a stack-allocated handle scope.
3702
+ v8::HandleScope handle_scope(isolate);
3703
+ // Create a new context.
3704
+ v8::Local<v8::Context> context = v8::Context::New(isolate);
3705
+ // Enter the context for compiling and running the hello world script.
3706
+ v8::Context::Scope context_scope(context);
3707
+ // Create a string containing the JavaScript source code.
3708
+ v8::Local<v8::String> source =
3709
+ v8::String::NewFromUtf8(isolate, "'Hello World!'",
3710
+ v8::NewStringType::kNormal).ToLocalChecked();
3711
+ // Compile the source code.
3712
+ v8::Local<v8::Script> script = v8::Script::Compile(context, source).ToLocalChecked();
3713
+ // Run the script to get the result.
3714
+ v8::Local<v8::Value> result = script->Run(context).ToLocalChecked();
3715
+ // Convert the result to an UTF8 string and print it.
3716
+ v8::String::Utf8Value utf8(isolate, result);
3717
+ printf("%s\n", *utf8);
3718
+ }
3719
+
3720
+ // Dispose the isolate and tear down V8.
3721
+ isolate->Dispose();
3722
+ v8::V8::Dispose();
3723
+ v8::V8::ShutdownPlatform();
3724
+ delete create_params.array_buffer_allocator;
3725
+ return 0;
3726
+ }
3727
+ </source>
3728
+
3663
3729
----
3664
3730
3665
3731
= 18 (其它) =
0 commit comments