]>
Commit | Line | Data |
---|---|---|
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" | |
31 | #include "JSCInlines.h" | |
32 | ||
33 | namespace JSC { | |
34 | ||
35 | const ClassInfo JSBoundFunction::s_info = { "Function", &Base::s_info, 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) | |
46 | args.append(boundArgs->getIndexQuickly(i)); | |
47 | for (unsigned i = 0; i < exec->argumentCount(); ++i) | |
48 | args.append(exec->uncheckedArgument(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 = 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) | |
66 | args.append(boundArgs->getIndexQuickly(i)); | |
67 | for (unsigned i = 0; i < exec->argumentCount(); ++i) | |
68 | args.append(exec->uncheckedArgument(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(VM& vm, JSGlobalObject* globalObject, JSObject* targetFunction, JSValue boundThis, JSValue boundArgs, int length, const String& name) | |
78 | { | |
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); | |
84 | ||
85 | function->finishCreation(vm, executable, length, name); | |
86 | return function; | |
87 | } | |
88 | ||
89 | bool JSBoundFunction::customHasInstance(JSObject* object, ExecState* exec, JSValue value) | |
90 | { | |
91 | return jsCast<JSBoundFunction*>(object)->m_targetFunction->hasInstance(exec, value); | |
92 | } | |
93 | ||
94 | JSBoundFunction::JSBoundFunction(VM& vm, JSGlobalObject* globalObject, Structure* structure, JSObject* targetFunction, JSValue boundThis, JSValue boundArgs) | |
95 | : Base(vm, globalObject, structure) | |
96 | , m_targetFunction(vm, this, targetFunction) | |
97 | , m_boundThis(vm, this, boundThis) | |
98 | , m_boundArgs(vm, this, boundArgs) | |
99 | { | |
100 | } | |
101 | ||
102 | void JSBoundFunction::finishCreation(VM& vm, NativeExecutable* executable, int length, const String& name) | |
103 | { | |
104 | Base::finishCreation(vm, executable, length, name); | |
105 | ASSERT(inherits(info())); | |
106 | ||
107 | putDirectNonIndexAccessor(vm, vm.propertyNames->arguments, globalObject()->throwTypeErrorGetterSetter(vm), DontDelete | DontEnum | Accessor); | |
108 | putDirectNonIndexAccessor(vm, vm.propertyNames->caller, globalObject()->throwTypeErrorGetterSetter(vm), DontDelete | DontEnum | Accessor); | |
109 | } | |
110 | ||
111 | void JSBoundFunction::visitChildren(JSCell* cell, SlotVisitor& visitor) | |
112 | { | |
113 | JSBoundFunction* thisObject = jsCast<JSBoundFunction*>(cell); | |
114 | ASSERT_GC_OBJECT_INHERITS(thisObject, info()); | |
115 | Base::visitChildren(thisObject, visitor); | |
116 | ||
117 | visitor.append(&thisObject->m_targetFunction); | |
118 | visitor.append(&thisObject->m_boundThis); | |
119 | visitor.append(&thisObject->m_boundArgs); | |
120 | } | |
121 | ||
122 | } // namespace JSC |