Commit c5d77733 authored by mike@belshe.com's avatar mike@belshe.com

Expose Cloning through API.


Review URL: http://codereview.chromium.org/42339

git-svn-id: http://v8.googlecode.com/svn/branches/bleeding_edge@1540 ce2b1a6d-e550-0410-aec6-3dcde31c8c00
parent 4b928a49
......@@ -1076,6 +1076,12 @@ class V8EXPORT Object : public Value {
*/
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:
......
......@@ -1919,6 +1919,17 @@ void v8::Object::TurnOnAccessCheck() {
}
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);
}
......
......@@ -5803,3 +5803,38 @@ TEST(RegExpInterruption) {
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")));
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment