aboutsummaryrefslogtreecommitdiff
path: root/samples
diff options
context:
space:
mode:
authorRubin Xu <rubinxu@google.com>2021-02-10 00:04:48 +0000
committerRubin Xu <rubinxu@google.com>2021-02-18 16:15:29 +0000
commit7314a9fc107bf9db96327cccfaa9b890e529b018 (patch)
tree086f3e6a7993bb346e943ed1aec7ad134fbc8f43 /samples
parent0c21ca9a7bd91fd88eabecfa9b6a528b51330149 (diff)
downloadv8-7314a9fc107bf9db96327cccfaa9b890e529b018.tar.gz
Upgrade V8 to 8.8.278.14
Bug: 162604069 Bug: 167389063 Test: gts-tradefed run gts-dev --module GtsGmscoreHostTestCases --test com.google.android.gts.devicepolicy.DeviceOwnerTest#testProxyPacProxyTest Test: m -j proxy_resolver_v8_unittest && adb sync && adb shell \ /data/nativetest/proxy_resolver_v8_unittest/proxy_resolver_v8_unittest Change-Id: Ifb09923b9d7f6d8990fb062d7dc0294edf2c098e
Diffstat (limited to 'samples')
-rw-r--r--samples/cppgc/cppgc-for-v8-embedders.cc70
-rw-r--r--samples/cppgc/cppgc-standalone.cc64
-rw-r--r--samples/hello-world.cc9
-rw-r--r--samples/process.cc48
-rw-r--r--samples/shell.cc71
5 files changed, 179 insertions, 83 deletions
diff --git a/samples/cppgc/cppgc-for-v8-embedders.cc b/samples/cppgc/cppgc-for-v8-embedders.cc
new file mode 100644
index 00000000..b4d7ed9e
--- /dev/null
+++ b/samples/cppgc/cppgc-for-v8-embedders.cc
@@ -0,0 +1,70 @@
+// Copyright 2020 the V8 project authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+#include <include/cppgc/allocation.h>
+#include <include/cppgc/default-platform.h>
+#include <include/cppgc/garbage-collected.h>
+#include <include/cppgc/heap.h>
+#include <include/cppgc/member.h>
+#include <include/cppgc/platform.h>
+#include <include/cppgc/visitor.h>
+#include <include/v8.h>
+
+#include <iostream>
+#include <memory>
+#include <string>
+
+/**
+ * This sample program shows how to set up a stand-alone cppgc heap as an
+ * embedder of V8. Most importantly, this example shows how to reuse V8's
+ * platform for cppgc.
+ */
+
+/**
+ * Simple string rope to illustrate allocation and garbage collection below. The
+ * rope keeps the next parts alive via regular managed reference.
+ */
+class Rope final : public cppgc::GarbageCollected<Rope> {
+ public:
+ explicit Rope(std::string part, Rope* next = nullptr)
+ : part_(part), next_(next) {}
+
+ void Trace(cppgc::Visitor* visitor) const { visitor->Trace(next_); }
+
+ private:
+ std::string part_;
+ cppgc::Member<Rope> next_;
+
+ friend std::ostream& operator<<(std::ostream& os, const Rope& rope);
+};
+
+std::ostream& operator<<(std::ostream& os, const Rope& rope) {
+ os << rope.part_;
+ if (rope.next_) {
+ os << *rope.next_;
+ }
+ return os;
+}
+
+int main(int argc, char* argv[]) {
+ // Create a platform that is used by cppgc::Heap for execution and backend
+ // allocation.
+ auto cppgc_platform = std::make_shared<cppgc::DefaultPlatform>();
+ // Initialize the process. This must happen before any cppgc::Heap::Create()
+ // calls.
+ cppgc::InitializeProcess(cppgc_platform->GetPageAllocator());
+ // Create a managed heap.
+ std::unique_ptr<cppgc::Heap> heap = cppgc::Heap::Create(cppgc_platform);
+ // Allocate a string rope on the managed heap.
+ auto* greeting = cppgc::MakeGarbageCollected<Rope>(
+ heap->GetAllocationHandle(), "Hello ",
+ cppgc::MakeGarbageCollected<Rope>(heap->GetAllocationHandle(), "World!"));
+ // Manually trigger garbage collection. The object greeting is held alive
+ // through conservative stack scanning.
+ heap->ForceGarbageCollectionSlow("V8 embedders example", "Testing");
+ std::cout << *greeting << std::endl;
+ // Gracefully shutdown the process.
+ cppgc::ShutdownProcess();
+ return 0;
+}
diff --git a/samples/cppgc/cppgc-standalone.cc b/samples/cppgc/cppgc-standalone.cc
new file mode 100644
index 00000000..f8cb4020
--- /dev/null
+++ b/samples/cppgc/cppgc-standalone.cc
@@ -0,0 +1,64 @@
+// Copyright 2020 the V8 project authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+#include <cppgc/allocation.h>
+#include <cppgc/default-platform.h>
+#include <cppgc/garbage-collected.h>
+#include <cppgc/heap.h>
+#include <cppgc/member.h>
+#include <cppgc/visitor.h>
+
+#include <iostream>
+#include <memory>
+#include <string>
+
+/**
+ * This sample program shows how to set up a stand-alone cppgc heap.
+ */
+
+/**
+ * Simple string rope to illustrate allocation and garbage collection below.
+ * The rope keeps the next parts alive via regular managed reference.
+ */
+class Rope final : public cppgc::GarbageCollected<Rope> {
+ public:
+ explicit Rope(std::string part, Rope* next = nullptr)
+ : part_(part), next_(next) {}
+
+ void Trace(cppgc::Visitor* visitor) const { visitor->Trace(next_); }
+
+ private:
+ std::string part_;
+ cppgc::Member<Rope> next_;
+
+ friend std::ostream& operator<<(std::ostream& os, const Rope& rope) {
+ os << rope.part_;
+ if (rope.next_) {
+ os << *rope.next_;
+ }
+ return os;
+ }
+};
+
+int main(int argc, char* argv[]) {
+ // Create a default platform that is used by cppgc::Heap for execution and
+ // backend allocation.
+ auto cppgc_platform = std::make_shared<cppgc::DefaultPlatform>();
+ // Initialize the process. This must happen before any
+ // cppgc::Heap::Create() calls.
+ cppgc::InitializeProcess(cppgc_platform->GetPageAllocator());
+ // Create a managed heap.
+ std::unique_ptr<cppgc::Heap> heap = cppgc::Heap::Create(cppgc_platform);
+ // Allocate a string rope on the managed heap.
+ auto* greeting = cppgc::MakeGarbageCollected<Rope>(
+ heap->GetAllocationHandle(), "Hello ",
+ cppgc::MakeGarbageCollected<Rope>(heap->GetAllocationHandle(), "World!"));
+ // Manually trigger garbage collection. The object greeting is held alive
+ // through conservative stack scanning.
+ heap->ForceGarbageCollectionSlow("CppGC stand-alone example", "Testing");
+ std::cout << *greeting << std::endl;
+ // Gracefully shutdown the process.
+ cppgc::ShutdownProcess();
+ return 0;
+}
diff --git a/samples/hello-world.cc b/samples/hello-world.cc
index d75dcb3c..6e506475 100644
--- a/samples/hello-world.cc
+++ b/samples/hello-world.cc
@@ -37,9 +37,7 @@ int main(int argc, char* argv[]) {
{
// Create a string containing the JavaScript source code.
v8::Local<v8::String> source =
- v8::String::NewFromUtf8(isolate, "'Hello' + ', World!'",
- v8::NewStringType::kNormal)
- .ToLocalChecked();
+ v8::String::NewFromUtf8Literal(isolate, "'Hello' + ', World!'");
// Compile the source code.
v8::Local<v8::Script> script =
@@ -63,7 +61,7 @@ int main(int argc, char* argv[]) {
// get_local 1
// i32.add)
//
- const char* csource = R"(
+ 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,
@@ -77,8 +75,7 @@ int main(int argc, char* argv[]) {
// Create a string containing the JavaScript source code.
v8::Local<v8::String> source =
- v8::String::NewFromUtf8(isolate, csource, v8::NewStringType::kNormal)
- .ToLocalChecked();
+ v8::String::NewFromUtf8Literal(isolate, csource);
// Compile the source code.
v8::Local<v8::Script> script =
diff --git a/samples/process.cc b/samples/process.cc
index 9af1c0b2..dec0d570 100644
--- a/samples/process.cc
+++ b/samples/process.cc
@@ -183,8 +183,7 @@ bool JsHttpRequestProcessor::Initialize(map<string, string>* opts,
// Create a template for the global object where we set the
// built-in global functions.
Local<ObjectTemplate> global = ObjectTemplate::New(GetIsolate());
- global->Set(String::NewFromUtf8(GetIsolate(), "log", NewStringType::kNormal)
- .ToLocalChecked(),
+ global->Set(GetIsolate(), "log",
FunctionTemplate::New(GetIsolate(), LogCallback));
// Each processor gets its own context so different processors don't
@@ -210,8 +209,7 @@ bool JsHttpRequestProcessor::Initialize(map<string, string>* opts,
// The script compiled and ran correctly. Now we fetch out the
// Process function from the global object.
Local<String> process_name =
- String::NewFromUtf8(GetIsolate(), "Process", NewStringType::kNormal)
- .ToLocalChecked();
+ String::NewFromUtf8Literal(GetIsolate(), "Process");
Local<Value> process_val;
// If there is no Process function, or if it is not a function,
// bail out
@@ -276,17 +274,13 @@ bool JsHttpRequestProcessor::InstallMaps(map<string, string>* opts,
// Set the options object as a property on the global object.
context->Global()
- ->Set(context,
- String::NewFromUtf8(GetIsolate(), "options", NewStringType::kNormal)
- .ToLocalChecked(),
+ ->Set(context, String::NewFromUtf8Literal(GetIsolate(), "options"),
opts_obj)
.FromJust();
Local<Object> output_obj = WrapMap(output);
context->Global()
- ->Set(context,
- String::NewFromUtf8(GetIsolate(), "output", NewStringType::kNormal)
- .ToLocalChecked(),
+ ->Set(context, String::NewFromUtf8Literal(GetIsolate(), "output"),
output_obj)
.FromJust();
@@ -563,21 +557,17 @@ Local<ObjectTemplate> JsHttpRequestProcessor::MakeRequestTemplate(
// Add accessors for each of the fields of the request.
result->SetAccessor(
- String::NewFromUtf8(isolate, "path", NewStringType::kInternalized)
- .ToLocalChecked(),
+ String::NewFromUtf8Literal(isolate, "path", NewStringType::kInternalized),
GetPath);
+ result->SetAccessor(String::NewFromUtf8Literal(isolate, "referrer",
+ NewStringType::kInternalized),
+ GetReferrer);
result->SetAccessor(
- String::NewFromUtf8(isolate, "referrer", NewStringType::kInternalized)
- .ToLocalChecked(),
- GetReferrer);
- result->SetAccessor(
- String::NewFromUtf8(isolate, "host", NewStringType::kInternalized)
- .ToLocalChecked(),
+ String::NewFromUtf8Literal(isolate, "host", NewStringType::kInternalized),
GetHost);
- result->SetAccessor(
- String::NewFromUtf8(isolate, "userAgent", NewStringType::kInternalized)
- .ToLocalChecked(),
- GetUserAgent);
+ result->SetAccessor(String::NewFromUtf8Literal(isolate, "userAgent",
+ NewStringType::kInternalized),
+ GetUserAgent);
// Again, return the result through the current handle scope.
return handle_scope.Escape(result);
@@ -676,19 +666,17 @@ StringHttpRequest kSampleRequests[kSampleSize] = {
StringHttpRequest("/", "localhost", "yahoo.com", "firefox")
};
-
-bool ProcessEntries(v8::Platform* platform, HttpRequestProcessor* processor,
- int count, StringHttpRequest* reqs) {
+bool ProcessEntries(v8::Isolate* isolate, v8::Platform* platform,
+ HttpRequestProcessor* processor, int count,
+ StringHttpRequest* reqs) {
for (int i = 0; i < count; i++) {
bool result = processor->Process(&reqs[i]);
- while (v8::platform::PumpMessageLoop(platform, Isolate::GetCurrent()))
- continue;
+ while (v8::platform::PumpMessageLoop(platform, isolate)) continue;
if (!result) return false;
}
return true;
}
-
void PrintMap(map<string, string>* m) {
for (map<string, string>::iterator i = m->begin(); i != m->end(); i++) {
pair<string, string> entry = *i;
@@ -727,7 +715,9 @@ int main(int argc, char* argv[]) {
fprintf(stderr, "Error initializing processor.\n");
return 1;
}
- if (!ProcessEntries(platform.get(), &processor, kSampleSize, kSampleRequests))
+ if (!ProcessEntries(isolate, platform.get(), &processor, kSampleSize,
+ kSampleRequests)) {
return 1;
+ }
PrintMap(&output);
}
diff --git a/samples/shell.cc b/samples/shell.cc
index 81b02872..70450296 100644
--- a/samples/shell.cc
+++ b/samples/shell.cc
@@ -108,28 +108,15 @@ v8::Local<v8::Context> CreateShellContext(v8::Isolate* isolate) {
// Create a template for the global object.
v8::Local<v8::ObjectTemplate> global = v8::ObjectTemplate::New(isolate);
// Bind the global 'print' function to the C++ Print callback.
- global->Set(
- v8::String::NewFromUtf8(isolate, "print", v8::NewStringType::kNormal)
- .ToLocalChecked(),
- v8::FunctionTemplate::New(isolate, Print));
+ global->Set(isolate, "print", v8::FunctionTemplate::New(isolate, Print));
// Bind the global 'read' function to the C++ Read callback.
- global->Set(v8::String::NewFromUtf8(
- isolate, "read", v8::NewStringType::kNormal).ToLocalChecked(),
- v8::FunctionTemplate::New(isolate, Read));
+ global->Set(isolate, "read", v8::FunctionTemplate::New(isolate, Read));
// Bind the global 'load' function to the C++ Load callback.
- global->Set(v8::String::NewFromUtf8(
- isolate, "load", v8::NewStringType::kNormal).ToLocalChecked(),
- v8::FunctionTemplate::New(isolate, Load));
+ global->Set(isolate, "load", v8::FunctionTemplate::New(isolate, Load));
// Bind the 'quit' function
- global->Set(v8::String::NewFromUtf8(
- isolate, "quit", v8::NewStringType::kNormal).ToLocalChecked(),
- v8::FunctionTemplate::New(isolate, Quit));
+ global->Set(isolate, "quit", v8::FunctionTemplate::New(isolate, Quit));
// Bind the 'version' function
- global->Set(
- v8::String::NewFromUtf8(isolate, "version", v8::NewStringType::kNormal)
- .ToLocalChecked(),
- v8::FunctionTemplate::New(isolate, Version));
-
+ global->Set(isolate, "version", v8::FunctionTemplate::New(isolate, Version));
return v8::Context::New(isolate, NULL, global);
}
@@ -161,22 +148,19 @@ void Print(const v8::FunctionCallbackInfo<v8::Value>& args) {
void Read(const v8::FunctionCallbackInfo<v8::Value>& args) {
if (args.Length() != 1) {
args.GetIsolate()->ThrowException(
- v8::String::NewFromUtf8(args.GetIsolate(), "Bad parameters",
- v8::NewStringType::kNormal).ToLocalChecked());
+ v8::String::NewFromUtf8Literal(args.GetIsolate(), "Bad parameters"));
return;
}
v8::String::Utf8Value file(args.GetIsolate(), args[0]);
if (*file == NULL) {
- args.GetIsolate()->ThrowException(
- v8::String::NewFromUtf8(args.GetIsolate(), "Error loading file",
- v8::NewStringType::kNormal).ToLocalChecked());
+ args.GetIsolate()->ThrowException(v8::String::NewFromUtf8Literal(
+ args.GetIsolate(), "Error loading file"));
return;
}
v8::Local<v8::String> source;
if (!ReadFile(args.GetIsolate(), *file).ToLocal(&source)) {
- args.GetIsolate()->ThrowException(
- v8::String::NewFromUtf8(args.GetIsolate(), "Error loading file",
- v8::NewStringType::kNormal).ToLocalChecked());
+ args.GetIsolate()->ThrowException(v8::String::NewFromUtf8Literal(
+ args.GetIsolate(), "Error loading file"));
return;
}
@@ -191,22 +175,19 @@ void Load(const v8::FunctionCallbackInfo<v8::Value>& args) {
v8::HandleScope handle_scope(args.GetIsolate());
v8::String::Utf8Value file(args.GetIsolate(), args[i]);
if (*file == NULL) {
- args.GetIsolate()->ThrowException(
- v8::String::NewFromUtf8(args.GetIsolate(), "Error loading file",
- v8::NewStringType::kNormal).ToLocalChecked());
+ args.GetIsolate()->ThrowException(v8::String::NewFromUtf8Literal(
+ args.GetIsolate(), "Error loading file"));
return;
}
v8::Local<v8::String> source;
if (!ReadFile(args.GetIsolate(), *file).ToLocal(&source)) {
- args.GetIsolate()->ThrowException(
- v8::String::NewFromUtf8(args.GetIsolate(), "Error loading file",
- v8::NewStringType::kNormal).ToLocalChecked());
+ args.GetIsolate()->ThrowException(v8::String::NewFromUtf8Literal(
+ args.GetIsolate(), "Error loading file"));
return;
}
if (!ExecuteString(args.GetIsolate(), source, args[i], false, false)) {
- args.GetIsolate()->ThrowException(
- v8::String::NewFromUtf8(args.GetIsolate(), "Error executing file",
- v8::NewStringType::kNormal).ToLocalChecked());
+ args.GetIsolate()->ThrowException(v8::String::NewFromUtf8Literal(
+ args.GetIsolate(), "Error executing file"));
return;
}
}
@@ -228,8 +209,8 @@ void Quit(const v8::FunctionCallbackInfo<v8::Value>& args) {
void Version(const v8::FunctionCallbackInfo<v8::Value>& args) {
args.GetReturnValue().Set(
- v8::String::NewFromUtf8(args.GetIsolate(), v8::V8::GetVersion(),
- v8::NewStringType::kNormal).ToLocalChecked());
+ v8::String::NewFromUtf8(args.GetIsolate(), v8::V8::GetVersion())
+ .ToLocalChecked());
}
@@ -276,12 +257,9 @@ int RunMain(v8::Isolate* isolate, v8::Platform* platform, int argc,
} else if (strcmp(str, "-e") == 0 && i + 1 < argc) {
// Execute argument given to -e option directly.
v8::Local<v8::String> file_name =
- v8::String::NewFromUtf8(isolate, "unnamed",
- v8::NewStringType::kNormal).ToLocalChecked();
+ v8::String::NewFromUtf8Literal(isolate, "unnamed");
v8::Local<v8::String> source;
- if (!v8::String::NewFromUtf8(isolate, argv[++i],
- v8::NewStringType::kNormal)
- .ToLocal(&source)) {
+ if (!v8::String::NewFromUtf8(isolate, argv[++i]).ToLocal(&source)) {
return 1;
}
bool success = ExecuteString(isolate, source, file_name, false, true);
@@ -290,8 +268,7 @@ int RunMain(v8::Isolate* isolate, v8::Platform* platform, int argc,
} else {
// Use all other arguments as names of files to load and run.
v8::Local<v8::String> file_name =
- v8::String::NewFromUtf8(isolate, str, v8::NewStringType::kNormal)
- .ToLocalChecked();
+ v8::String::NewFromUtf8(isolate, str).ToLocalChecked();
v8::Local<v8::String> source;
if (!ReadFile(isolate, str).ToLocal(&source)) {
fprintf(stderr, "Error reading '%s'\n", str);
@@ -313,8 +290,7 @@ void RunShell(v8::Local<v8::Context> context, v8::Platform* platform) {
// Enter the execution environment before evaluating any code.
v8::Context::Scope context_scope(context);
v8::Local<v8::String> name(
- v8::String::NewFromUtf8(context->GetIsolate(), "(shell)",
- v8::NewStringType::kNormal).ToLocalChecked());
+ v8::String::NewFromUtf8Literal(context->GetIsolate(), "(shell)"));
while (true) {
char buffer[kBufferSize];
fprintf(stderr, "> ");
@@ -323,8 +299,7 @@ void RunShell(v8::Local<v8::Context> context, v8::Platform* platform) {
v8::HandleScope handle_scope(context->GetIsolate());
ExecuteString(
context->GetIsolate(),
- v8::String::NewFromUtf8(context->GetIsolate(), str,
- v8::NewStringType::kNormal).ToLocalChecked(),
+ v8::String::NewFromUtf8(context->GetIsolate(), str).ToLocalChecked(),
name, true, true);
while (v8::platform::PumpMessageLoop(platform, context->GetIsolate()))
continue;