From 58839390c3001a90e387c4211620e38ca8c53717 Mon Sep 17 00:00:00 2001 From: Adam Klein Date: Mon, 17 Nov 2014 12:29:00 -0800 Subject: [PATCH] Add a cctest for using a C++ FunctionCallback as an Object.observe observer R=rossberg@chromium.org BUG=v8:3076 LOG=n Review URL: https://codereview.chromium.org/733483003 Cr-Commit-Position: refs/heads/master@{#25384} --- test/cctest/test-object-observe.cc | 43 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 43 insertions(+) diff --git a/test/cctest/test-object-observe.cc b/test/cctest/test-object-observe.cc index 8851d88..7e9992c 100644 --- a/test/cctest/test-object-observe.cc +++ b/test/cctest/test-object-observe.cc @@ -762,3 +762,46 @@ TEST(DontLeakContextOnNotifierPerformChange) { CcTest::isolate()->ContextDisposedNotification(); CheckSurvivingGlobalObjectsCount(1); } + + +static void ObserverCallback(const FunctionCallbackInfo& args) { + *static_cast(Handle::Cast(args.Data())->Value()) = + Handle::Cast(args[0])->Length(); +} + + +TEST(ObjectObserveCallsCppFunction) { + Isolate* isolate = CcTest::isolate(); + HandleScope scope(isolate); + LocalContext context(isolate); + int numRecordsSent = 0; + Handle observer = + Function::New(CcTest::isolate(), ObserverCallback, + External::New(isolate, &numRecordsSent)); + context->Global()->Set(String::NewFromUtf8(CcTest::isolate(), "observer"), + observer); + CompileRun( + "var obj = {};" + "Object.observe(obj, observer);" + "obj.foo = 1;" + "obj.bar = 2;"); + CHECK_EQ(2, numRecordsSent); +} + + +TEST(ObjectObserveCallsFunctionTemplateInstance) { + Isolate* isolate = CcTest::isolate(); + HandleScope scope(isolate); + LocalContext context(isolate); + int numRecordsSent = 0; + Handle tmpl = FunctionTemplate::New( + isolate, ObserverCallback, External::New(isolate, &numRecordsSent)); + context->Global()->Set(String::NewFromUtf8(CcTest::isolate(), "observer"), + tmpl->GetFunction()); + CompileRun( + "var obj = {};" + "Object.observe(obj, observer);" + "obj.foo = 1;" + "obj.bar = 2;"); + CHECK_EQ(2, numRecordsSent); +} -- 2.7.4