]>
Commit | Line | Data |
---|---|---|
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" | |
81345200 | 31 | #include "JSCInlines.h" |
6fe7ccc8 A |
32 | |
33 | namespace JSC { | |
34 | ||
6fe7ccc8 A |
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 = 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 | 47 | for (unsigned i = 0; i < exec->argumentCount(); ++i) |
81345200 | 48 | args.append(exec->uncheckedArgument(i)); |
6fe7ccc8 A |
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 = 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 | 67 | for (unsigned i = 0; i < exec->argumentCount(); ++i) |
81345200 | 68 | args.append(exec->uncheckedArgument(i)); |
6fe7ccc8 A |
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 | ||
81345200 | 77 | JSBoundFunction* JSBoundFunction::create(VM& vm, 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; | |
81345200 A |
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); | |
6fe7ccc8 | 84 | |
81345200 | 85 | function->finishCreation(vm, executable, length, name); |
6fe7ccc8 A |
86 | return function; |
87 | } | |
88 | ||
93a37866 A |
89 | void JSBoundFunction::destroy(JSCell* cell) |
90 | { | |
91 | static_cast<JSBoundFunction*>(cell)->JSBoundFunction::~JSBoundFunction(); | |
92 | } | |
93 | ||
94 | bool 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 | ||
81345200 A |
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) | |
6fe7ccc8 A |
104 | { |
105 | } | |
106 | ||
81345200 | 107 | void JSBoundFunction::finishCreation(VM& vm, NativeExecutable* executable, int length, const String& name) |
6fe7ccc8 | 108 | { |
81345200 A |
109 | Base::finishCreation(vm, executable, length, name); |
110 | ASSERT(inherits(info())); | |
6fe7ccc8 | 111 | |
81345200 A |
112 | putDirectNonIndexAccessor(vm, vm.propertyNames->arguments, globalObject()->throwTypeErrorGetterSetter(vm), DontDelete | DontEnum | Accessor); |
113 | putDirectNonIndexAccessor(vm, vm.propertyNames->caller, globalObject()->throwTypeErrorGetterSetter(vm), DontDelete | DontEnum | Accessor); | |
6fe7ccc8 A |
114 | } |
115 | ||
116 | void JSBoundFunction::visitChildren(JSCell* cell, SlotVisitor& visitor) | |
117 | { | |
118 | JSBoundFunction* thisObject = jsCast<JSBoundFunction*>(cell); | |
81345200 | 119 | ASSERT_GC_OBJECT_INHERITS(thisObject, info()); |
6fe7ccc8 A |
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 |