Author: [email protected] Date: Wed Mar 18 12:24:15 2009 New Revision: 1540 Modified: branches/bleeding_edge/include/v8.h branches/bleeding_edge/src/api.cc branches/bleeding_edge/test/cctest/test-api.cc
Log: Expose Cloning through API. Review URL: http://codereview.chromium.org/42339 Modified: branches/bleeding_edge/include/v8.h ============================================================================== --- branches/bleeding_edge/include/v8.h (original) +++ branches/bleeding_edge/include/v8.h Wed Mar 18 12:24:15 2009 @@ -1076,6 +1076,12 @@ */ void TurnOnAccessCheck(); + /** + * Clone this object with a fast but shallow copy. Values will point + * to the same values as the original object. + */ + Local<Object> Clone(); + static Local<Object> New(); static Object* Cast(Value* obj); private: Modified: branches/bleeding_edge/src/api.cc ============================================================================== --- branches/bleeding_edge/src/api.cc (original) +++ branches/bleeding_edge/src/api.cc Wed Mar 18 12:24:15 2009 @@ -1919,6 +1919,17 @@ } +Local<v8::Object> v8::Object::Clone() { + ON_BAILOUT("v8::Object::Clone()", return Local<Object>()); + i::Handle<i::JSObject> self = Utils::OpenHandle(this); + EXCEPTION_PREAMBLE(); + i::Handle<i::JSObject> result = i::Copy(self); + has_pending_exception = result.is_null(); + EXCEPTION_BAILOUT_CHECK(Local<Object>()); + return Utils::ToLocal(result); +} + + Local<v8::Object> Function::NewInstance() const { return NewInstance(0, NULL); } Modified: branches/bleeding_edge/test/cctest/test-api.cc ============================================================================== --- branches/bleeding_edge/test/cctest/test-api.cc (original) +++ branches/bleeding_edge/test/cctest/test-api.cc Wed Mar 18 12:24:15 2009 @@ -5803,3 +5803,38 @@ local_env->Exit(); } + + +// Verify that we can clone an object +TEST(ObjectClone) { + v8::HandleScope scope; + LocalContext env; + + char* sample = + "var rv = {};" \ + "rv.alpha = 'hello';" \ + "rv.beta = 123;" \ + "rv;"; + + // Create an object, verify basics. + Local<Value> val = CompileRun(sample); + CHECK(val->IsObject()); + Local<v8::Object> obj = Local<v8::Object>::Cast(val); + obj->Set(v8_str("gamma"), v8_str("cloneme")); + + CHECK_EQ(v8_str("hello"), obj->Get(v8_str("alpha"))); + CHECK_EQ(v8::Integer::New(123), obj->Get(v8_str("beta"))); + CHECK_EQ(v8_str("cloneme"), obj->Get(v8_str("gamma"))); + + // Clone it. + Local<v8::Object> clone = obj->Clone(); + CHECK_EQ(v8_str("hello"), clone->Get(v8_str("alpha"))); + CHECK_EQ(v8::Integer::New(123), clone->Get(v8_str("beta"))); + CHECK_EQ(v8_str("cloneme"), clone->Get(v8_str("gamma"))); + + // Set a property on the clone, verify each object. + clone->Set(v8_str("beta"), v8::Integer::New(456)); + CHECK_EQ(v8::Integer::New(123), obj->Get(v8_str("beta"))); + CHECK_EQ(v8::Integer::New(456), clone->Get(v8_str("beta"))); +} + --~--~---------~--~----~------------~-------~--~----~ v8-dev mailing list [email protected] http://groups.google.com/group/v8-dev -~----------~----~----~----~------~----~------~--~---
