]> git.saurik.com Git - apple/javascriptcore.git/blame - runtime/JSBoundFunction.cpp
JavaScriptCore-1218.34.tar.gz
[apple/javascriptcore.git] / runtime / JSBoundFunction.cpp
CommitLineData
6fe7ccc8
A
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 "GetterSetter.h"
30#include "JSGlobalObject.h"
93a37866 31#include "Operations.h"
6fe7ccc8
A
32
33namespace JSC {
34
6fe7ccc8
A
35const ClassInfo JSBoundFunction::s_info = { "Function", &Base::s_info, 0, 0, CREATE_METHOD_TABLE(JSBoundFunction) };
36
37EncodedJSValue JSC_HOST_CALL boundFunctionCall(ExecState* exec)
38{
39 JSBoundFunction* boundFunction = jsCast<JSBoundFunction*>(exec->callee());
40
41 ASSERT(isJSArray(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)
93a37866 46 args.append(boundArgs->getIndexQuickly(i));
6fe7ccc8
A
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
57EncodedJSValue JSC_HOST_CALL boundFunctionConstruct(ExecState* exec)
58{
59 JSBoundFunction* boundFunction = jsCast<JSBoundFunction*>(exec->callee());
60
61 ASSERT(isJSArray(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)
93a37866 66 args.append(boundArgs->getIndexQuickly(i));
6fe7ccc8
A
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
93a37866 77JSBoundFunction* JSBoundFunction::create(ExecState* exec, JSGlobalObject* globalObject, JSObject* targetFunction, JSValue boundThis, JSValue boundArgs, int length, const String& name)
6fe7ccc8
A
78{
79 ConstructData constructData;
80 ConstructType constructType = JSC::getConstructData(targetFunction, constructData);
81 bool canConstruct = constructType != ConstructTypeNone;
93a37866 82 NativeExecutable* executable = exec->vm().getHostFunction(boundFunctionCall, canConstruct ? boundFunctionConstruct : callHostFunctionAsConstructor);
6fe7ccc8
A
83 JSBoundFunction* function = new (NotNull, allocateCell<JSBoundFunction>(*exec->heap())) JSBoundFunction(exec, globalObject, globalObject->boundFunctionStructure(), targetFunction, boundThis, boundArgs);
84
85 function->finishCreation(exec, executable, length, name);
86 return function;
87}
88
93a37866
A
89void JSBoundFunction::destroy(JSCell* cell)
90{
91 static_cast<JSBoundFunction*>(cell)->JSBoundFunction::~JSBoundFunction();
92}
93
94bool JSBoundFunction::customHasInstance(JSObject* object, ExecState* exec, JSValue value)
6fe7ccc8 95{
93a37866 96 return jsCast<JSBoundFunction*>(object)->m_targetFunction->hasInstance(exec, value);
6fe7ccc8
A
97}
98
99JSBoundFunction::JSBoundFunction(ExecState* exec, JSGlobalObject* globalObject, Structure* structure, JSObject* targetFunction, JSValue boundThis, JSValue boundArgs)
100 : Base(exec, globalObject, structure)
93a37866
A
101 , m_targetFunction(exec->vm(), this, targetFunction)
102 , m_boundThis(exec->vm(), this, boundThis)
103 , m_boundArgs(exec->vm(), this, boundArgs)
6fe7ccc8
A
104{
105}
106
93a37866 107void JSBoundFunction::finishCreation(ExecState* exec, NativeExecutable* executable, int length, const String& name)
6fe7ccc8
A
108{
109 Base::finishCreation(exec, executable, length, name);
110 ASSERT(inherits(&s_info));
111
93a37866
A
112 putDirectAccessor(exec, exec->propertyNames().arguments, globalObject()->throwTypeErrorGetterSetter(exec), DontDelete | DontEnum | Accessor);
113 putDirectAccessor(exec, exec->propertyNames().caller, globalObject()->throwTypeErrorGetterSetter(exec), DontDelete | DontEnum | Accessor);
6fe7ccc8
A
114}
115
116void JSBoundFunction::visitChildren(JSCell* cell, SlotVisitor& visitor)
117{
118 JSBoundFunction* thisObject = jsCast<JSBoundFunction*>(cell);
119 ASSERT_GC_OBJECT_INHERITS(thisObject, &s_info);
120 COMPILE_ASSERT(StructureFlags & OverridesVisitChildren, OverridesVisitChildrenWithoutSettingFlag);
121 ASSERT(thisObject->structure()->typeInfo().overridesVisitChildren());
122 Base::visitChildren(thisObject, visitor);
123
124 visitor.append(&thisObject->m_targetFunction);
125 visitor.append(&thisObject->m_boundThis);
126 visitor.append(&thisObject->m_boundArgs);
127}
128
129} // namespace JSC