~oif-team/ubuntu/natty/qt4-x11/xi2.1

« back to all changes in this revision

Viewing changes to src/3rdparty/webkit/JavaScriptCore/runtime/FunctionPrototype.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Alessandro Ghersi
  • Date: 2009-11-02 18:30:08 UTC
  • mfrom: (1.2.2 upstream)
  • mto: (15.2.5 experimental)
  • mto: This revision was merged to the branch mainline in revision 88.
  • Revision ID: james.westby@ubuntu.com-20091102183008-b6a4gcs128mvfb3m
Tags: upstream-4.6.0~beta1
ImportĀ upstreamĀ versionĀ 4.6.0~beta1

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
1
/*
2
2
 *  Copyright (C) 1999-2001 Harri Porten (porten@kde.org)
3
 
 *  Copyright (C) 2003, 2004, 2005, 2006, 2007, 2008 Apple Inc. All rights reserved.
 
3
 *  Copyright (C) 2003, 2004, 2005, 2006, 2007, 2008, 2009 Apple Inc. All rights reserved.
4
4
 *
5
5
 *  This library is free software; you can redistribute it and/or
6
6
 *  modify it under the terms of the GNU Lesser General Public
26
26
#include "JSFunction.h"
27
27
#include "JSString.h"
28
28
#include "Interpreter.h"
 
29
#include "Lexer.h"
29
30
#include "PrototypeFunction.h"
30
31
 
31
32
namespace JSC {
32
33
 
33
34
ASSERT_CLASS_FITS_IN_CELL(FunctionPrototype);
34
35
 
35
 
static JSValuePtr functionProtoFuncToString(ExecState*, JSObject*, JSValuePtr, const ArgList&);
36
 
static JSValuePtr functionProtoFuncApply(ExecState*, JSObject*, JSValuePtr, const ArgList&);
37
 
static JSValuePtr functionProtoFuncCall(ExecState*, JSObject*, JSValuePtr, const ArgList&);
 
36
static JSValue JSC_HOST_CALL functionProtoFuncToString(ExecState*, JSObject*, JSValue, const ArgList&);
 
37
static JSValue JSC_HOST_CALL functionProtoFuncApply(ExecState*, JSObject*, JSValue, const ArgList&);
 
38
static JSValue JSC_HOST_CALL functionProtoFuncCall(ExecState*, JSObject*, JSValue, const ArgList&);
38
39
 
39
 
FunctionPrototype::FunctionPrototype(ExecState* exec, PassRefPtr<Structure> structure)
 
40
FunctionPrototype::FunctionPrototype(ExecState* exec, NonNullPassRefPtr<Structure> structure)
40
41
    : InternalFunction(&exec->globalData(), structure, exec->propertyNames().nullIdentifier)
41
42
{
42
43
    putDirectWithoutTransition(exec->propertyNames().length, jsNumber(exec, 0), DontDelete | ReadOnly | DontEnum);
43
44
}
44
45
 
45
 
void FunctionPrototype::addFunctionProperties(ExecState* exec, Structure* prototypeFunctionStructure)
 
46
void FunctionPrototype::addFunctionProperties(ExecState* exec, Structure* prototypeFunctionStructure, NativeFunctionWrapper** callFunction, NativeFunctionWrapper** applyFunction)
46
47
{
47
 
    putDirectFunctionWithoutTransition(exec, new (exec) PrototypeFunction(exec, prototypeFunctionStructure, 0, exec->propertyNames().toString, functionProtoFuncToString), DontEnum);
48
 
    putDirectFunctionWithoutTransition(exec, new (exec) PrototypeFunction(exec, prototypeFunctionStructure, 2, exec->propertyNames().apply, functionProtoFuncApply), DontEnum);
49
 
    putDirectFunctionWithoutTransition(exec, new (exec) PrototypeFunction(exec, prototypeFunctionStructure, 1, exec->propertyNames().call, functionProtoFuncCall), DontEnum);
 
48
    putDirectFunctionWithoutTransition(exec, new (exec) NativeFunctionWrapper(exec, prototypeFunctionStructure, 0, exec->propertyNames().toString, functionProtoFuncToString), DontEnum);
 
49
    *applyFunction = new (exec) NativeFunctionWrapper(exec, prototypeFunctionStructure, 2, exec->propertyNames().apply, functionProtoFuncApply);
 
50
    putDirectFunctionWithoutTransition(exec, *applyFunction, DontEnum);
 
51
    *callFunction = new (exec) NativeFunctionWrapper(exec, prototypeFunctionStructure, 1, exec->propertyNames().call, functionProtoFuncCall);
 
52
    putDirectFunctionWithoutTransition(exec, *callFunction, DontEnum);
50
53
}
51
54
 
52
 
static JSValuePtr callFunctionPrototype(ExecState*, JSObject*, JSValuePtr, const ArgList&)
 
55
static JSValue JSC_HOST_CALL callFunctionPrototype(ExecState*, JSObject*, JSValue, const ArgList&)
53
56
{
54
57
    return jsUndefined();
55
58
}
63
66
 
64
67
// Functions
65
68
 
66
 
JSValuePtr functionProtoFuncToString(ExecState* exec, JSObject*, JSValuePtr thisValue, const ArgList&)
67
 
{
68
 
    if (thisValue->isObject(&JSFunction::info)) {
 
69
// Compatibility hack for the Optimost JavaScript library. (See <rdar://problem/6595040>.)
 
70
static inline void insertSemicolonIfNeeded(UString& functionBody)
 
71
{
 
72
    ASSERT(functionBody[0] == '{');
 
73
    ASSERT(functionBody[functionBody.size() - 1] == '}');
 
74
 
 
75
    for (size_t i = functionBody.size() - 2; i > 0; --i) {
 
76
        UChar ch = functionBody[i];
 
77
        if (!Lexer::isWhiteSpace(ch) && !Lexer::isLineTerminator(ch)) {
 
78
            if (ch != ';' && ch != '}')
 
79
                functionBody = functionBody.substr(0, i + 1) + ";" + functionBody.substr(i + 1, functionBody.size() - (i + 1));
 
80
            return;
 
81
        }
 
82
    }
 
83
}
 
84
 
 
85
JSValue JSC_HOST_CALL functionProtoFuncToString(ExecState* exec, JSObject*, JSValue thisValue, const ArgList&)
 
86
{
 
87
    if (thisValue.inherits(&JSFunction::info)) {
69
88
        JSFunction* function = asFunction(thisValue);
70
 
        return jsString(exec, "function " + function->name(&exec->globalData()) + "(" + function->body()->paramString() + ") " + function->body()->toSourceString());
 
89
        if (!function->isHostFunction()) {
 
90
            FunctionExecutable* executable = function->jsExecutable();
 
91
            UString sourceString = executable->source().toString();
 
92
            insertSemicolonIfNeeded(sourceString);
 
93
            return jsString(exec, "function " + function->name(&exec->globalData()) + "(" + executable->paramString() + ") " + sourceString);
 
94
        }
71
95
    }
72
96
 
73
 
    if (thisValue->isObject(&InternalFunction::info)) {
 
97
    if (thisValue.inherits(&InternalFunction::info)) {
74
98
        InternalFunction* function = asInternalFunction(thisValue);
75
99
        return jsString(exec, "function " + function->name(&exec->globalData()) + "() {\n    [native code]\n}");
76
100
    }
78
102
    return throwError(exec, TypeError);
79
103
}
80
104
 
81
 
JSValuePtr functionProtoFuncApply(ExecState* exec, JSObject*, JSValuePtr thisValue, const ArgList& args)
 
105
JSValue JSC_HOST_CALL functionProtoFuncApply(ExecState* exec, JSObject*, JSValue thisValue, const ArgList& args)
82
106
{
83
107
    CallData callData;
84
 
    CallType callType = thisValue->getCallData(callData);
 
108
    CallType callType = thisValue.getCallData(callData);
85
109
    if (callType == CallTypeNone)
86
110
        return throwError(exec, TypeError);
87
111
 
88
 
    JSValuePtr thisArg = args.at(exec, 0);
89
 
    JSValuePtr argArray = args.at(exec, 1);
90
 
 
91
 
    JSValuePtr applyThis;
92
 
    if (thisArg->isUndefinedOrNull())
93
 
        applyThis = exec->globalThisValue();
94
 
    else
95
 
        applyThis = thisArg->toObject(exec);
96
 
 
97
 
    ArgList applyArgs;
98
 
    if (!argArray->isUndefinedOrNull()) {
99
 
        if (!argArray->isObject())
 
112
    JSValue array = args.at(1);
 
113
 
 
114
    MarkedArgumentBuffer applyArgs;
 
115
    if (!array.isUndefinedOrNull()) {
 
116
        if (!array.isObject())
100
117
            return throwError(exec, TypeError);
101
 
        if (asObject(argArray)->classInfo() == &Arguments::info)
102
 
            asArguments(argArray)->fillArgList(exec, applyArgs);
103
 
        else if (exec->interpreter()->isJSArray(argArray))
104
 
            asArray(argArray)->fillArgList(exec, applyArgs);
105
 
        else if (asObject(argArray)->inherits(&JSArray::info)) {
106
 
            unsigned length = asArray(argArray)->get(exec, exec->propertyNames().length)->toUInt32(exec);
 
118
        if (asObject(array)->classInfo() == &Arguments::info)
 
119
            asArguments(array)->fillArgList(exec, applyArgs);
 
120
        else if (isJSArray(&exec->globalData(), array))
 
121
            asArray(array)->fillArgList(exec, applyArgs);
 
122
        else if (asObject(array)->inherits(&JSArray::info)) {
 
123
            unsigned length = asArray(array)->get(exec, exec->propertyNames().length).toUInt32(exec);
107
124
            for (unsigned i = 0; i < length; ++i)
108
 
                applyArgs.append(asArray(argArray)->get(exec, i));
 
125
                applyArgs.append(asArray(array)->get(exec, i));
109
126
        } else
110
127
            return throwError(exec, TypeError);
111
128
    }
112
129
 
113
 
    return call(exec, thisValue, callType, callData, applyThis, applyArgs);
 
130
    return call(exec, thisValue, callType, callData, args.at(0), applyArgs);
114
131
}
115
132
 
116
 
JSValuePtr functionProtoFuncCall(ExecState* exec, JSObject*, JSValuePtr thisValue, const ArgList& args)
 
133
JSValue JSC_HOST_CALL functionProtoFuncCall(ExecState* exec, JSObject*, JSValue thisValue, const ArgList& args)
117
134
{
118
135
    CallData callData;
119
 
    CallType callType = thisValue->getCallData(callData);
 
136
    CallType callType = thisValue.getCallData(callData);
120
137
    if (callType == CallTypeNone)
121
138
        return throwError(exec, TypeError);
122
139
 
123
 
    JSValuePtr thisArg = args.at(exec, 0);
124
 
 
125
 
    JSObject* callThis;
126
 
    if (thisArg->isUndefinedOrNull())
127
 
        callThis = exec->globalThisValue();
128
 
    else
129
 
        callThis = thisArg->toObject(exec);
130
 
 
131
 
    ArgList argsTail;
132
 
    args.getSlice(1, argsTail);
133
 
    return call(exec, thisValue, callType, callData, callThis, argsTail);
 
140
    ArgList callArgs;
 
141
    args.getSlice(1, callArgs);
 
142
    return call(exec, thisValue, callType, callData, args.at(0), callArgs);
134
143
}
135
144
 
136
145
} // namespace JSC