tizen beta release
[framework/web/webkit-efl.git] / Source / JavaScriptCore / runtime / JSBoundFunction.cpp
1 /*
2  * Copyright (C) 2011 Apple Inc. All rights reserved.
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions
6  * are met:
7  * 1. Redistributions of source code must retain the above copyright
8  *    notice, this list of conditions and the following disclaimer.
9  * 2. Redistributions in binary form must reproduce the above copyright
10  *    notice, this list of conditions and the following disclaimer in the
11  *    documentation and/or other materials provided with the distribution.
12  *
13  * THIS SOFTWARE IS PROVIDED BY APPLE INC. ``AS IS'' AND ANY
14  * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
15  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
16  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL APPLE INC. OR
17  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
18  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
19  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
20  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
21  * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
22  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
23  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 
24  */
25
26 #include "config.h"
27 #include "JSBoundFunction.h"
28
29 #include "JSGlobalObject.h"
30
31 namespace JSC {
32
33 ASSERT_CLASS_FITS_IN_CELL(JSBoundFunction);
34
35 const ClassInfo JSBoundFunction::s_info = { "Function", &Base::s_info, 0, 0, CREATE_METHOD_TABLE(JSBoundFunction) };
36
37 EncodedJSValue JSC_HOST_CALL boundFunctionCall(ExecState* exec)
38 {
39     JSBoundFunction* boundFunction = static_cast<JSBoundFunction*>(exec->callee());
40
41     ASSERT(isJSArray(&exec->globalData(), boundFunction->boundArgs())); // Currently this is true!
42     JSArray* boundArgs = asArray(boundFunction->boundArgs());
43
44     MarkedArgumentBuffer args;
45     for (unsigned i = 0; i < boundArgs->length(); ++i)
46         args.append(boundArgs->getIndex(i));
47     for (unsigned i = 0; i < exec->argumentCount(); ++i)
48         args.append(exec->argument(i));
49
50     JSObject* targetFunction = boundFunction->targetFunction();
51     CallData callData;
52     CallType callType = getCallData(targetFunction, callData);
53     ASSERT(callType != CallTypeNone);
54     return JSValue::encode(call(exec, targetFunction, callType, callData, boundFunction->boundThis(), args));
55 }
56
57 EncodedJSValue JSC_HOST_CALL boundFunctionConstruct(ExecState* exec)
58 {
59     JSBoundFunction* boundFunction = static_cast<JSBoundFunction*>(exec->callee());
60
61     ASSERT(isJSArray(&exec->globalData(), boundFunction->boundArgs())); // Currently this is true!
62     JSArray* boundArgs = asArray(boundFunction->boundArgs());
63
64     MarkedArgumentBuffer args;
65     for (unsigned i = 0; i < boundArgs->length(); ++i)
66         args.append(boundArgs->getIndex(i));
67     for (unsigned i = 0; i < exec->argumentCount(); ++i)
68         args.append(exec->argument(i));
69
70     JSObject* targetFunction = boundFunction->targetFunction();
71     ConstructData constructData;
72     ConstructType constructType = getConstructData(targetFunction, constructData);
73     ASSERT(constructType != ConstructTypeNone);
74     return JSValue::encode(construct(exec, targetFunction, constructType, constructData, args));
75 }
76
77 JSBoundFunction* JSBoundFunction::create(ExecState* exec, JSGlobalObject* globalObject, JSObject* targetFunction, JSValue boundThis, JSValue boundArgs, int length, const Identifier& name)
78 {
79     ConstructData constructData;
80     ConstructType constructType = JSC::getConstructData(targetFunction, constructData);
81     bool canConstruct = constructType != ConstructTypeNone;
82
83     NativeExecutable* executable = exec->globalData().getHostFunction(boundFunctionCall, canConstruct ? boundFunctionConstruct : callHostFunctionAsConstructor);
84     JSBoundFunction* function = new (allocateCell<JSBoundFunction>(*exec->heap())) JSBoundFunction(exec, globalObject, globalObject->boundFunctionStructure(), targetFunction, boundThis, boundArgs);
85
86     function->finishCreation(exec, executable, length, name);
87     return function;
88 }
89
90 bool JSBoundFunction::hasInstance(JSObject* object, ExecState* exec, JSValue value, JSValue)
91 {
92     JSBoundFunction* thisObject = jsCast<JSBoundFunction*>(object);
93     // FIXME: our instanceof implementation will have already (incorrectly) performed
94     // a [[Get]] of .prototype from the bound function object, which is incorrect!
95     // https://bugs.webkit.org/show_bug.cgi?id=68656
96     JSValue proto = thisObject->m_targetFunction->get(exec, exec->propertyNames().prototype);
97     return thisObject->m_targetFunction->methodTable()->hasInstance(thisObject->m_targetFunction.get(), exec, value, proto);
98 }
99
100 JSBoundFunction::JSBoundFunction(ExecState* exec, JSGlobalObject* globalObject, Structure* structure, JSObject* targetFunction, JSValue boundThis, JSValue boundArgs)
101     : Base(exec, globalObject, structure)
102     , m_targetFunction(exec->globalData(), this, targetFunction)
103     , m_boundThis(exec->globalData(), this, boundThis)
104     , m_boundArgs(exec->globalData(), this, boundArgs)
105 {
106 }
107
108 void JSBoundFunction::finishCreation(ExecState* exec, NativeExecutable* executable, int length, const Identifier& name)
109 {
110     Base::finishCreation(exec, executable, length, name);
111     ASSERT(inherits(&s_info));
112
113     initializeGetterSetterProperty(exec, exec->propertyNames().arguments, globalObject()->throwTypeErrorGetterSetter(exec), DontDelete | DontEnum | Getter | Setter);
114     initializeGetterSetterProperty(exec, exec->propertyNames().caller, globalObject()->throwTypeErrorGetterSetter(exec), DontDelete | DontEnum | Getter | Setter);
115 }
116
117 void JSBoundFunction::visitChildren(JSCell* cell, SlotVisitor& visitor)
118 {
119     JSBoundFunction* thisObject = jsCast<JSBoundFunction*>(cell);
120     ASSERT_GC_OBJECT_INHERITS(thisObject, &s_info);
121     COMPILE_ASSERT(StructureFlags & OverridesVisitChildren, OverridesVisitChildrenWithoutSettingFlag);
122     ASSERT(thisObject->structure()->typeInfo().overridesVisitChildren());
123     Base::visitChildren(thisObject, visitor);
124
125     visitor.append(&thisObject->m_targetFunction);
126     visitor.append(&thisObject->m_boundThis);
127     visitor.append(&thisObject->m_boundArgs);
128 }
129
130 } // namespace JSC