Commit 7ed401ad authored by oliver@apple.com's avatar oliver@apple.com

Implement ES5 Object.create function

https://bugs.webkit.org/show_bug.cgi?id=29524

Reviewed by Maciej Stachowiak

Implement Object.create.  Very simple patch, effectively Object.defineProperties
only creating the target object itself.

git-svn-id: http://svn.webkit.org/repository/webkit/trunk@48568 268f45cc-cd09-0410-ab3c-d52691b4dbfc
parent 8cb3f20c
2009-09-19 Oliver Hunt <oliver@apple.com>
Reviewed by Maciej Stachowiak.
Implement ES5 Object.create function
https://bugs.webkit.org/show_bug.cgi?id=29524
Implement Object.create. Very simple patch, effectively Object.defineProperties
only creating the target object itself.
* runtime/CommonIdentifiers.h:
* runtime/ObjectConstructor.cpp:
(JSC::ObjectConstructor::ObjectConstructor):
(JSC::objectConstructorCreate):
2009-09-19 Dan Bernstein <mitz@apple.com>
Fix clean debug builds.
......
......@@ -39,6 +39,7 @@
macro(compile) \
macro(configurable) \
macro(constructor) \
macro(create) \
macro(defineProperty) \
macro(defineProperties) \
macro(enumerable) \
......
......@@ -39,6 +39,7 @@ static JSValue JSC_HOST_CALL objectConstructorGetOwnPropertyDescriptor(ExecState
static JSValue JSC_HOST_CALL objectConstructorKeys(ExecState*, JSObject*, JSValue, const ArgList&);
static JSValue JSC_HOST_CALL objectConstructorDefineProperty(ExecState*, JSObject*, JSValue, const ArgList&);
static JSValue JSC_HOST_CALL objectConstructorDefineProperties(ExecState*, JSObject*, JSValue, const ArgList&);
static JSValue JSC_HOST_CALL objectConstructorCreate(ExecState*, JSObject*, JSValue, const ArgList&);
ObjectConstructor::ObjectConstructor(ExecState* exec, PassRefPtr<Structure> structure, ObjectPrototype* objectPrototype, Structure* prototypeFunctionStructure)
: InternalFunction(&exec->globalData(), structure, Identifier(exec, "Object"))
......@@ -54,6 +55,7 @@ ObjectConstructor::ObjectConstructor(ExecState* exec, PassRefPtr<Structure> stru
putDirectFunctionWithoutTransition(exec, new (exec) NativeFunctionWrapper(exec, prototypeFunctionStructure, 1, exec->propertyNames().keys, objectConstructorKeys), DontEnum);
putDirectFunctionWithoutTransition(exec, new (exec) NativeFunctionWrapper(exec, prototypeFunctionStructure, 3, exec->propertyNames().defineProperty, objectConstructorDefineProperty), DontEnum);
putDirectFunctionWithoutTransition(exec, new (exec) NativeFunctionWrapper(exec, prototypeFunctionStructure, 2, exec->propertyNames().defineProperties, objectConstructorDefineProperties), DontEnum);
putDirectFunctionWithoutTransition(exec, new (exec) NativeFunctionWrapper(exec, prototypeFunctionStructure, 2, exec->propertyNames().create, objectConstructorCreate), DontEnum);
}
// ECMA 15.2.2
......@@ -282,4 +284,17 @@ JSValue JSC_HOST_CALL objectConstructorDefineProperties(ExecState* exec, JSObjec
return defineProperties(exec, asObject(args.at(0)), asObject(args.at(1)));
}
JSValue JSC_HOST_CALL objectConstructorCreate(ExecState* exec, JSObject*, JSValue, const ArgList& args)
{
if (!args.at(0).isObject() && !args.at(0).isNull())
return throwError(exec, TypeError, "Object prototype may only be an Object or null.");
JSObject* newObject = constructEmptyObject(exec);
newObject->setPrototype(args.at(0));
if (args.at(1).isUndefined())
return newObject;
if (!args.at(1).isObject())
return throwError(exec, TypeError, "Property descriptor list must be an Object.");
return defineProperties(exec, newObject, asObject(args.at(1)));
}
} // namespace JSC
2009-09-19 Oliver Hunt <oliver@apple.com>
Reviewed by Maciej Stachowiak.
Implement ES5 Object.create function
https://bugs.webkit.org/show_bug.cgi?id=29524
Add tests for Object.create API
* fast/js/Object-create-expected.txt: Added.
* fast/js/Object-create.html: Added.
* fast/js/resources/Object-create.js: Added.
2009-09-19 Sam Weinig <sam@webkit.org>
Reviewed by Oliver Hunt.
......
Test to ensure correct behaviour of Object.defineProperties
On success, you will see a series of "PASS" messages, followed by "TEST COMPLETE".
PASS Object.create() threw exception TypeError: Object prototype may only be an Object or null..
PASS Object.create('a string') threw exception TypeError: Object prototype may only be an Object or null..
PASS Object.create({}, 'a string') threw exception TypeError: Property descriptor list must be an Object..
PASS Object.create(null, 'a string') threw exception TypeError: Property descriptor list must be an Object..
PASS JSON.stringify(Object.create(null,{property:{value:'foo', enumerable:true}, property2:{value:'foo', enumerable:true}})) is '{"property":"foo","property2":"foo"}'
PASS JSON.stringify(Object.create({},{property:{value:'foo', enumerable:true}, property2:{value:'foo', enumerable:true}})) is '{"property":"foo","property2":"foo"}'
PASS JSON.stringify(Object.create({},{property:{value:'foo'}, property2:{value:'foo', enumerable:true}})) is '{"property2":"foo"}'
PASS JSON.stringify(Object.create(null,{property:{value:'foo'}, property2:{value:'foo', enumerable:true}})) is '{"property2":"foo"}'
PASS Object.getPrototypeOf(Object.create(Array.prototype)) is Array.prototype
PASS Object.getPrototypeOf(Object.create(null)) is null
PASS successfullyParsed is true
TEST COMPLETE
<!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML//EN">
<html>
<head>
<link rel="stylesheet" href="resources/js-test-style.css">
<script src="resources/js-test-pre.js"></script>
</head>
<body>
<p id="description"></p>
<div id="console"></div>
<script src="resources/Object-create.js"></script>
<script src="resources/js-test-post.js"></script>
</body>
</html>
description("Test to ensure correct behaviour of Object.defineProperties");
shouldThrow("Object.create()");
shouldThrow("Object.create('a string')");
shouldThrow("Object.create({}, 'a string')");
shouldThrow("Object.create(null, 'a string')");
shouldBe("JSON.stringify(Object.create(null,{property:{value:'foo', enumerable:true}, property2:{value:'foo', enumerable:true}}))", '\'{"property":"foo","property2":"foo"}\'');
shouldBe("JSON.stringify(Object.create({},{property:{value:'foo', enumerable:true}, property2:{value:'foo', enumerable:true}}))", '\'{"property":"foo","property2":"foo"}\'');
shouldBe("JSON.stringify(Object.create({},{property:{value:'foo'}, property2:{value:'foo', enumerable:true}}))", '\'{"property2":"foo"}\'');
shouldBe("JSON.stringify(Object.create(null,{property:{value:'foo'}, property2:{value:'foo', enumerable:true}}))", '\'{"property2":"foo"}\'');
shouldBe("Object.getPrototypeOf(Object.create(Array.prototype))", "Array.prototype");
shouldBe("Object.getPrototypeOf(Object.create(null))", "null");
successfullyParsed = true;
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