Skip to content

Instantly share code, notes, and snippets.

@gabrielschulhof
Last active July 10, 2018 18:27
Show Gist options
  • Save gabrielschulhof/2d97f082daf66c93db0578ee440bfeaa to your computer and use it in GitHub Desktop.
Save gabrielschulhof/2d97f082daf66c93db0578ee440bfeaa to your computer and use it in GitHub Desktop.
V8 inheritance test illustration
diff --git a/test/cctest/BUILD.gn b/test/cctest/BUILD.gn
index 1aa4a07ca4..9f0eaeeb3a 100644
--- a/test/cctest/BUILD.gn
+++ b/test/cctest/BUILD.gn
@@ -192,6 +192,7 @@ v8_source_set("cctest_sources") {
"test-heap-profiler.cc",
"test-identity-map.cc",
"test-inobject-slack-tracking.cc",
+ "test-inheritance.cc",
"test-intl.cc",
"test-isolate-independent-builtins.cc",
"test-liveedit.cc",
diff --git a/test/cctest/test-inheritance.cc b/test/cctest/test-inheritance.cc
new file mode 100644
index 0000000000..19c7115d31
--- /dev/null
+++ b/test/cctest/test-inheritance.cc
@@ -0,0 +1,66 @@
+#include "src/v8.h"
+#include "test/cctest/cctest.h"
+
+static const char js_script[] =
+"const x = function doSubclassCallTest(superClass) {"
+" class Subclass extends superClass {};"
+" const inst = new Subclass();"
+" inst.superclassMethod();"
+" return true;"
+"}; x";
+
+static const char js_script_proto[] =
+"const x = function doSubclassCallTest(superClass) {"
+" const Subclass = function Subclass() {};"
+" Object.setPrototypeOf(Subclass, superClass);"
+" Object.setPrototypeOf(Subclass.prototype, superClass.prototype);"
+" const inst = new Subclass();"
+" inst.superclassMethod();"
+" return true;"
+"}; x";
+
+namespace v8 {
+
+static void SuperclassConstructor(const FunctionCallbackInfo<Value> &info) {}
+static void SuperclassMethod(const FunctionCallbackInfo<Value> &info) {}
+
+namespace test_inheritance {
+
+static void do_test(const char* script) {
+ CcTest::InitializeVM();
+ Isolate* isolate = CcTest::isolate();
+ HandleScope scope(isolate);
+
+ auto context = isolate->GetCurrentContext();
+ auto tpl = FunctionTemplate::New(isolate, SuperclassConstructor);
+ auto name =
+ String::NewFromUtf8(isolate, "Superclass", NewStringType::kNormal)
+ .ToLocalChecked();
+ tpl->SetClassName(name);
+ auto method_tpl = FunctionTemplate::New(isolate, SuperclassMethod,
+ Local<Value>(), Signature::New(isolate, tpl));
+ tpl->PrototypeTemplate()->Set(isolate, "superclassMethod", method_tpl);
+
+ auto script_source =
+ String::NewFromUtf8(isolate, script, NewStringType::kNormal)
+ .ToLocalChecked();
+
+ auto test_script = Script::Compile(context, script_source).ToLocalChecked();
+ auto test_function = test_script->Run(context).ToLocalChecked();
+ auto function_to_pass = tpl->GetFunction().As<Value>();
+ auto result = test_function.As<Function>()->Call(context,
+ Undefined(isolate).As<Value>(), 1, &function_to_pass);
+ CHECK(!result.IsEmpty());
+ CHECK(result.ToLocalChecked().As<Boolean>()->Value());
+}
+
+TEST(Inheritance) {
+ do_test(js_script);
+}
+
+TEST(ProtoInheritance) {
+ do_test(js_script_proto);
+}
+
+} // namespace test_inheritance
+} // namespace v8
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment