2 * Copyright (C) 2011 Apple Inc. All rights reserved.
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
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.
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.
27 #include "JSBoundFunction.h"
29 #include "GetterSetter.h"
30 #include "JSGlobalObject.h"
31 #include "JSCInlines.h"
35 const ClassInfo
JSBoundFunction::s_info
= { "Function", &Base::s_info
, 0, 0, CREATE_METHOD_TABLE(JSBoundFunction
) };
37 EncodedJSValue JSC_HOST_CALL
boundFunctionCall(ExecState
* exec
)
39 JSBoundFunction
* boundFunction
= jsCast
<JSBoundFunction
*>(exec
->callee());
41 ASSERT(isJSArray(boundFunction
->boundArgs())); // Currently this is true!
42 JSArray
* boundArgs
= asArray(boundFunction
->boundArgs());
44 MarkedArgumentBuffer args
;
45 for (unsigned i
= 0; i
< boundArgs
->length(); ++i
)
46 args
.append(boundArgs
->getIndexQuickly(i
));
47 for (unsigned i
= 0; i
< exec
->argumentCount(); ++i
)
48 args
.append(exec
->uncheckedArgument(i
));
50 JSObject
* targetFunction
= boundFunction
->targetFunction();
52 CallType callType
= getCallData(targetFunction
, callData
);
53 ASSERT(callType
!= CallTypeNone
);
54 return JSValue::encode(call(exec
, targetFunction
, callType
, callData
, boundFunction
->boundThis(), args
));
57 EncodedJSValue JSC_HOST_CALL
boundFunctionConstruct(ExecState
* exec
)
59 JSBoundFunction
* boundFunction
= jsCast
<JSBoundFunction
*>(exec
->callee());
61 ASSERT(isJSArray(boundFunction
->boundArgs())); // Currently this is true!
62 JSArray
* boundArgs
= asArray(boundFunction
->boundArgs());
64 MarkedArgumentBuffer args
;
65 for (unsigned i
= 0; i
< boundArgs
->length(); ++i
)
66 args
.append(boundArgs
->getIndexQuickly(i
));
67 for (unsigned i
= 0; i
< exec
->argumentCount(); ++i
)
68 args
.append(exec
->uncheckedArgument(i
));
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
));
77 JSBoundFunction
* JSBoundFunction::create(VM
& vm
, JSGlobalObject
* globalObject
, JSObject
* targetFunction
, JSValue boundThis
, JSValue boundArgs
, int length
, const String
& name
)
79 ConstructData constructData
;
80 ConstructType constructType
= JSC::getConstructData(targetFunction
, constructData
);
81 bool canConstruct
= constructType
!= ConstructTypeNone
;
82 NativeExecutable
* executable
= vm
.getHostFunction(boundFunctionCall
, canConstruct
? boundFunctionConstruct
: callHostFunctionAsConstructor
);
83 JSBoundFunction
* function
= new (NotNull
, allocateCell
<JSBoundFunction
>(vm
.heap
)) JSBoundFunction(vm
, globalObject
, globalObject
->boundFunctionStructure(), targetFunction
, boundThis
, boundArgs
);
85 function
->finishCreation(vm
, executable
, length
, name
);
89 void JSBoundFunction::destroy(JSCell
* cell
)
91 static_cast<JSBoundFunction
*>(cell
)->JSBoundFunction::~JSBoundFunction();
94 bool JSBoundFunction::customHasInstance(JSObject
* object
, ExecState
* exec
, JSValue value
)
96 return jsCast
<JSBoundFunction
*>(object
)->m_targetFunction
->hasInstance(exec
, value
);
99 JSBoundFunction::JSBoundFunction(VM
& vm
, JSGlobalObject
* globalObject
, Structure
* structure
, JSObject
* targetFunction
, JSValue boundThis
, JSValue boundArgs
)
100 : Base(vm
, globalObject
, structure
)
101 , m_targetFunction(vm
, this, targetFunction
)
102 , m_boundThis(vm
, this, boundThis
)
103 , m_boundArgs(vm
, this, boundArgs
)
107 void JSBoundFunction::finishCreation(VM
& vm
, NativeExecutable
* executable
, int length
, const String
& name
)
109 Base::finishCreation(vm
, executable
, length
, name
);
110 ASSERT(inherits(info()));
112 putDirectNonIndexAccessor(vm
, vm
.propertyNames
->arguments
, globalObject()->throwTypeErrorGetterSetter(vm
), DontDelete
| DontEnum
| Accessor
);
113 putDirectNonIndexAccessor(vm
, vm
.propertyNames
->caller
, globalObject()->throwTypeErrorGetterSetter(vm
), DontDelete
| DontEnum
| Accessor
);
116 void JSBoundFunction::visitChildren(JSCell
* cell
, SlotVisitor
& visitor
)
118 JSBoundFunction
* thisObject
= jsCast
<JSBoundFunction
*>(cell
);
119 ASSERT_GC_OBJECT_INHERITS(thisObject
, info());
120 COMPILE_ASSERT(StructureFlags
& OverridesVisitChildren
, OverridesVisitChildrenWithoutSettingFlag
);
121 ASSERT(thisObject
->structure()->typeInfo().overridesVisitChildren());
122 Base::visitChildren(thisObject
, visitor
);
124 visitor
.append(&thisObject
->m_targetFunction
);
125 visitor
.append(&thisObject
->m_boundThis
);
126 visitor
.append(&thisObject
->m_boundArgs
);