summaryrefslogtreecommitdiff
path: root/deps/v8/samples/hello-world.cc
diff options
context:
space:
mode:
Diffstat (limited to 'deps/v8/samples/hello-world.cc')
-rw-r--r--deps/v8/samples/hello-world.cc73
1 files changed, 57 insertions, 16 deletions
diff --git a/deps/v8/samples/hello-world.cc b/deps/v8/samples/hello-world.cc
index ab6f0dd8bf..d75dcb3c3c 100644
--- a/deps/v8/samples/hello-world.cc
+++ b/deps/v8/samples/hello-world.cc
@@ -34,22 +34,63 @@ int main(int argc, char* argv[]) {
// Enter the context for compiling and running the hello world script.
v8::Context::Scope context_scope(context);
- // Create a string containing the JavaScript source code.
- v8::Local<v8::String> source =
- v8::String::NewFromUtf8(isolate, "'Hello' + ', World!'",
- v8::NewStringType::kNormal)
- .ToLocalChecked();
-
- // Compile the source code.
- v8::Local<v8::Script> script =
- v8::Script::Compile(context, source).ToLocalChecked();
-
- // Run the script to get the result.
- v8::Local<v8::Value> result = script->Run(context).ToLocalChecked();
-
- // Convert the result to an UTF8 string and print it.
- v8::String::Utf8Value utf8(isolate, result);
- printf("%s\n", *utf8);
+ {
+ // Create a string containing the JavaScript source code.
+ v8::Local<v8::String> source =
+ v8::String::NewFromUtf8(isolate, "'Hello' + ', World!'",
+ v8::NewStringType::kNormal)
+ .ToLocalChecked();
+
+ // Compile the source code.
+ v8::Local<v8::Script> script =
+ v8::Script::Compile(context, source).ToLocalChecked();
+
+ // Run the script to get the result.
+ v8::Local<v8::Value> result = script->Run(context).ToLocalChecked();
+
+ // Convert the result to an UTF8 string and print it.
+ v8::String::Utf8Value utf8(isolate, result);
+ printf("%s\n", *utf8);
+ }
+
+ {
+ // Use the JavaScript API to generate a WebAssembly module.
+ //
+ // |bytes| contains the binary format for the following module:
+ //
+ // (func (export "add") (param i32 i32) (result i32)
+ // get_local 0
+ // get_local 1
+ // i32.add)
+ //
+ const char* csource = R"(
+ let bytes = new Uint8Array([
+ 0x00, 0x61, 0x73, 0x6d, 0x01, 0x00, 0x00, 0x00, 0x01, 0x07, 0x01,
+ 0x60, 0x02, 0x7f, 0x7f, 0x01, 0x7f, 0x03, 0x02, 0x01, 0x00, 0x07,
+ 0x07, 0x01, 0x03, 0x61, 0x64, 0x64, 0x00, 0x00, 0x0a, 0x09, 0x01,
+ 0x07, 0x00, 0x20, 0x00, 0x20, 0x01, 0x6a, 0x0b
+ ]);
+ let module = new WebAssembly.Module(bytes);
+ let instance = new WebAssembly.Instance(module);
+ instance.exports.add(3, 4);
+ )";
+
+ // Create a string containing the JavaScript source code.
+ v8::Local<v8::String> source =
+ v8::String::NewFromUtf8(isolate, csource, v8::NewStringType::kNormal)
+ .ToLocalChecked();
+
+ // Compile the source code.
+ v8::Local<v8::Script> script =
+ v8::Script::Compile(context, source).ToLocalChecked();
+
+ // Run the script to get the result.
+ v8::Local<v8::Value> result = script->Run(context).ToLocalChecked();
+
+ // Convert the result to a uint32 and print it.
+ uint32_t number = result->Uint32Value(context).ToChecked();
+ printf("3 + 4 = %u\n", number);
+ }
}
// Dispose the isolate and tear down V8.