~ubuntu-branches/ubuntu/vivid/qtdeclarative-opensource-src-gles/vivid-proposed

« back to all changes in this revision

Viewing changes to .pc/QML-Compilation-unit-caching-and-JIT-changes.patch/src/qml/jsruntime/qv4functionobject.cpp

  • Committer: Package Import Robot
  • Author(s): Timo Jyrinki
  • Date: 2015-02-03 16:35:14 UTC
  • Revision ID: package-import@ubuntu.com-20150203163514-u0cmjz42p04pu2lx
Tags: 5.3.2-0ubuntu4
Sync package with qtdeclarative-opensource-src - 5.3.2-3ubuntu3

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/****************************************************************************
 
2
**
 
3
** Copyright (C) 2013 Digia Plc and/or its subsidiary(-ies).
 
4
** Contact: http://www.qt-project.org/legal
 
5
**
 
6
** This file is part of the QtQml module of the Qt Toolkit.
 
7
**
 
8
** $QT_BEGIN_LICENSE:LGPL$
 
9
** Commercial License Usage
 
10
** Licensees holding valid commercial Qt licenses may use this file in
 
11
** accordance with the commercial license agreement provided with the
 
12
** Software or, alternatively, in accordance with the terms contained in
 
13
** a written agreement between you and Digia.  For licensing terms and
 
14
** conditions see http://qt.digia.com/licensing.  For further information
 
15
** use the contact form at http://qt.digia.com/contact-us.
 
16
**
 
17
** GNU Lesser General Public License Usage
 
18
** Alternatively, this file may be used under the terms of the GNU Lesser
 
19
** General Public License version 2.1 as published by the Free Software
 
20
** Foundation and appearing in the file LICENSE.LGPL included in the
 
21
** packaging of this file.  Please review the following information to
 
22
** ensure the GNU Lesser General Public License version 2.1 requirements
 
23
** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
 
24
**
 
25
** In addition, as a special exception, Digia gives you certain additional
 
26
** rights.  These rights are described in the Digia Qt LGPL Exception
 
27
** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
 
28
**
 
29
** GNU General Public License Usage
 
30
** Alternatively, this file may be used under the terms of the GNU
 
31
** General Public License version 3.0 as published by the Free Software
 
32
** Foundation and appearing in the file LICENSE.GPL included in the
 
33
** packaging of this file.  Please review the following information to
 
34
** ensure the GNU General Public License version 3.0 requirements will be
 
35
** met: http://www.gnu.org/copyleft/gpl.html.
 
36
**
 
37
**
 
38
** $QT_END_LICENSE$
 
39
**
 
40
****************************************************************************/
 
41
 
 
42
#include "qv4object_p.h"
 
43
#include "qv4jsir_p.h"
 
44
#include "qv4isel_p.h"
 
45
#include "qv4objectproto_p.h"
 
46
#include "qv4stringobject_p.h"
 
47
#include "qv4function_p.h"
 
48
#include "qv4mm_p.h"
 
49
 
 
50
#include "qv4arrayobject_p.h"
 
51
#include "qv4scopedvalue_p.h"
 
52
 
 
53
#include <private/qqmljsengine_p.h>
 
54
#include <private/qqmljslexer_p.h>
 
55
#include <private/qqmljsparser_p.h>
 
56
#include <private/qqmljsast_p.h>
 
57
#include <private/qqmlcontextwrapper_p.h>
 
58
#include <private/qqmlengine_p.h>
 
59
#include <qv4jsir_p.h>
 
60
#include <qv4codegen_p.h>
 
61
#include "private/qlocale_tools_p.h"
 
62
 
 
63
#include <QtCore/qmath.h>
 
64
#include <QtCore/QDebug>
 
65
#include <cassert>
 
66
#include <typeinfo>
 
67
#include <iostream>
 
68
#include <algorithm>
 
69
#include "qv4alloca_p.h"
 
70
#include "qv4profiling_p.h"
 
71
 
 
72
using namespace QV4;
 
73
 
 
74
 
 
75
DEFINE_OBJECT_VTABLE(FunctionObject);
 
76
 
 
77
FunctionObject::FunctionObject(ExecutionContext *scope, const StringRef name, bool createProto)
 
78
    : Object(scope->engine->functionClass)
 
79
    , scope(scope)
 
80
    , function(0)
 
81
{
 
82
     init(name, createProto);
 
83
}
 
84
 
 
85
FunctionObject::FunctionObject(ExecutionContext *scope, const QString &name, bool createProto)
 
86
    : Object(scope->engine->functionClass)
 
87
    , scope(scope)
 
88
    , function(0)
 
89
{
 
90
    Scope s(scope);
 
91
    ScopedValue protectThis(s, this);
 
92
    ScopedString n(s, s.engine->newString(name));
 
93
    init(n, createProto);
 
94
}
 
95
 
 
96
FunctionObject::FunctionObject(ExecutionContext *scope, const ReturnedValue name)
 
97
    : Object(scope->engine->functionClass)
 
98
    , scope(scope)
 
99
    , function(0)
 
100
{
 
101
    Scope s(scope);
 
102
    ScopedValue protectThis(s, this);
 
103
    ScopedString n(s, name);
 
104
    init(n, false);
 
105
}
 
106
 
 
107
FunctionObject::FunctionObject(InternalClass *ic)
 
108
    : Object(ic)
 
109
    , scope(ic->engine->rootContext)
 
110
    , function(0)
 
111
{
 
112
    needsActivation = false;
 
113
    strictMode = false;
 
114
    memberData[Index_Prototype] = Encode::undefined();
 
115
}
 
116
 
 
117
FunctionObject::~FunctionObject()
 
118
{
 
119
    if (function)
 
120
        function->compilationUnit->deref();
 
121
}
 
122
 
 
123
void FunctionObject::init(const StringRef n, bool createProto)
 
124
{
 
125
    Scope s(internalClass->engine);
 
126
    ScopedValue protectThis(s, this);
 
127
 
 
128
    needsActivation = true;
 
129
    strictMode = false;
 
130
 
 
131
    if (createProto) {
 
132
        Scoped<Object> proto(s, scope->engine->newObject(scope->engine->protoClass));
 
133
        proto->memberData[Index_ProtoConstructor] = this->asReturnedValue();
 
134
        memberData[Index_Prototype] = proto.asReturnedValue();
 
135
    } else {
 
136
        memberData[Index_Prototype] = Encode::undefined();
 
137
    }
 
138
 
 
139
    ScopedValue v(s, n.asReturnedValue());
 
140
    defineReadonlyProperty(scope->engine->id_name, v);
 
141
}
 
142
 
 
143
ReturnedValue FunctionObject::name()
 
144
{
 
145
    return get(scope->engine->id_name);
 
146
}
 
147
 
 
148
 
 
149
ReturnedValue FunctionObject::newInstance()
 
150
{
 
151
    Scope scope(internalClass->engine);
 
152
    ScopedCallData callData(scope, 0);
 
153
    return construct(callData);
 
154
}
 
155
 
 
156
ReturnedValue FunctionObject::construct(Managed *that, CallData *)
 
157
{
 
158
    that->internalClass->engine->currentContext()->throwTypeError();
 
159
    return Encode::undefined();
 
160
}
 
161
 
 
162
ReturnedValue FunctionObject::call(Managed *, CallData *)
 
163
{
 
164
    return Encode::undefined();
 
165
}
 
166
 
 
167
void FunctionObject::markObjects(Managed *that, ExecutionEngine *e)
 
168
{
 
169
    FunctionObject *o = static_cast<FunctionObject *>(that);
 
170
    if (o->scope)
 
171
        o->scope->mark(e);
 
172
 
 
173
    Object::markObjects(that, e);
 
174
}
 
175
 
 
176
FunctionObject *FunctionObject::createScriptFunction(ExecutionContext *scope, Function *function, bool createProto)
 
177
{
 
178
    if (function->needsActivation() ||
 
179
        function->compiledFunction->flags & CompiledData::Function::HasCatchOrWith ||
 
180
        function->compiledFunction->nFormals > QV4::Global::ReservedArgumentCount ||
 
181
        function->isNamedExpression())
 
182
        return new (scope->engine->memoryManager) ScriptFunction(scope, function);
 
183
    return new (scope->engine->memoryManager) SimpleScriptFunction(scope, function, createProto);
 
184
}
 
185
 
 
186
DEFINE_OBJECT_VTABLE(FunctionCtor);
 
187
 
 
188
FunctionCtor::FunctionCtor(ExecutionContext *scope)
 
189
    : FunctionObject(scope, QStringLiteral("Function"))
 
190
{
 
191
    setVTable(staticVTable());
 
192
}
 
193
 
 
194
// 15.3.2
 
195
ReturnedValue FunctionCtor::construct(Managed *that, CallData *callData)
 
196
{
 
197
    FunctionCtor *f = static_cast<FunctionCtor *>(that);
 
198
    ExecutionEngine *v4 = f->internalClass->engine;
 
199
    ExecutionContext *ctx = v4->currentContext();
 
200
    QString arguments;
 
201
    QString body;
 
202
    if (callData->argc > 0) {
 
203
        for (int i = 0; i < callData->argc - 1; ++i) {
 
204
            if (i)
 
205
                arguments += QLatin1String(", ");
 
206
            arguments += callData->args[i].toString(ctx)->toQString();
 
207
        }
 
208
        body = callData->args[callData->argc - 1].toString(ctx)->toQString();
 
209
    }
 
210
    if (ctx->engine->hasException)
 
211
        return Encode::undefined();
 
212
 
 
213
    QString function = QLatin1String("function(") + arguments + QLatin1String("){") + body + QLatin1String("}");
 
214
 
 
215
    QQmlJS::Engine ee, *engine = &ee;
 
216
    QQmlJS::Lexer lexer(engine);
 
217
    lexer.setCode(function, 1, false);
 
218
    QQmlJS::Parser parser(engine);
 
219
 
 
220
    const bool parsed = parser.parseExpression();
 
221
 
 
222
    if (!parsed)
 
223
        return v4->currentContext()->throwSyntaxError(QLatin1String("Parse error"));
 
224
 
 
225
    using namespace QQmlJS::AST;
 
226
    FunctionExpression *fe = QQmlJS::AST::cast<FunctionExpression *>(parser.rootNode());
 
227
    if (!fe)
 
228
        return v4->currentContext()->throwSyntaxError(QLatin1String("Parse error"));
 
229
 
 
230
    IR::Module module(v4->debugger != 0);
 
231
 
 
232
    QQmlJS::RuntimeCodegen cg(v4->currentContext(), f->strictMode);
 
233
    cg.generateFromFunctionExpression(QString(), function, fe, &module);
 
234
 
 
235
    QV4::Compiler::JSUnitGenerator jsGenerator(&module);
 
236
    QScopedPointer<EvalInstructionSelection> isel(v4->iselFactory->create(QQmlEnginePrivate::get(v4), v4->executableAllocator, &module, &jsGenerator));
 
237
    QV4::CompiledData::CompilationUnit *compilationUnit = isel->compile();
 
238
    QV4::Function *vmf = compilationUnit->linkToEngine(v4);
 
239
 
 
240
    return FunctionObject::createScriptFunction(v4->rootContext, vmf)->asReturnedValue();
 
241
}
 
242
 
 
243
// 15.3.1: This is equivalent to new Function(...)
 
244
ReturnedValue FunctionCtor::call(Managed *that, CallData *callData)
 
245
{
 
246
    return construct(that, callData);
 
247
}
 
248
 
 
249
FunctionPrototype::FunctionPrototype(InternalClass *ic)
 
250
    : FunctionObject(ic)
 
251
{
 
252
}
 
253
 
 
254
void FunctionPrototype::init(ExecutionEngine *engine, ObjectRef ctor)
 
255
{
 
256
    Scope scope(engine);
 
257
    ScopedObject o(scope);
 
258
 
 
259
    ctor->defineReadonlyProperty(engine->id_length, Primitive::fromInt32(1));
 
260
    ctor->defineReadonlyProperty(engine->id_prototype, (o = this));
 
261
 
 
262
    defineReadonlyProperty(engine->id_length, Primitive::fromInt32(0));
 
263
    defineDefaultProperty(QStringLiteral("constructor"), (o = ctor));
 
264
    defineDefaultProperty(engine->id_toString, method_toString, 0);
 
265
    defineDefaultProperty(QStringLiteral("apply"), method_apply, 2);
 
266
    defineDefaultProperty(QStringLiteral("call"), method_call, 1);
 
267
    defineDefaultProperty(QStringLiteral("bind"), method_bind, 1);
 
268
 
 
269
}
 
270
 
 
271
ReturnedValue FunctionPrototype::method_toString(CallContext *ctx)
 
272
{
 
273
    FunctionObject *fun = ctx->callData->thisObject.asFunctionObject();
 
274
    if (!fun)
 
275
        return ctx->throwTypeError();
 
276
 
 
277
    return ctx->engine->newString(QStringLiteral("function() { [code] }"))->asReturnedValue();
 
278
}
 
279
 
 
280
ReturnedValue FunctionPrototype::method_apply(CallContext *ctx)
 
281
{
 
282
    Scope scope(ctx);
 
283
    FunctionObject *o = ctx->callData->thisObject.asFunctionObject();
 
284
    if (!o)
 
285
        return ctx->throwTypeError();
 
286
 
 
287
    ScopedValue arg(scope, ctx->argument(1));
 
288
 
 
289
    ScopedObject arr(scope, arg);
 
290
 
 
291
    quint32 len;
 
292
    if (!arr) {
 
293
        len = 0;
 
294
        if (!arg->isNullOrUndefined())
 
295
            return ctx->throwTypeError();
 
296
    } else {
 
297
        len = arr->getLength();
 
298
    }
 
299
 
 
300
    ScopedCallData callData(scope, len);
 
301
 
 
302
    if (len) {
 
303
        if (arr->arrayType() != ArrayData::Simple || arr->protoHasArray() || arr->hasAccessorProperty) {
 
304
            for (quint32 i = 0; i < len; ++i)
 
305
                callData->args[i] = arr->getIndexed(i);
 
306
        } else {
 
307
            int alen = qMin(len, arr->arrayData->length());
 
308
            if (alen)
 
309
                memcpy(callData->args, arr->arrayData->data, alen*sizeof(Value));
 
310
            for (quint32 i = alen; i < len; ++i)
 
311
                callData->args[i] = Primitive::undefinedValue();
 
312
        }
 
313
    }
 
314
 
 
315
    callData->thisObject = ctx->argument(0);
 
316
    return o->call(callData);
 
317
}
 
318
 
 
319
ReturnedValue FunctionPrototype::method_call(CallContext *ctx)
 
320
{
 
321
    Scope scope(ctx);
 
322
 
 
323
    FunctionObject *o = ctx->callData->thisObject.asFunctionObject();
 
324
    if (!o)
 
325
        return ctx->throwTypeError();
 
326
 
 
327
    ScopedCallData callData(scope, ctx->callData->argc ? ctx->callData->argc - 1 : 0);
 
328
    if (ctx->callData->argc) {
 
329
        for (int i = 1; i < ctx->callData->argc; ++i)
 
330
            callData->args[i - 1] = ctx->callData->args[i];
 
331
    }
 
332
    callData->thisObject = ctx->argument(0);
 
333
    return o->call(callData);
 
334
}
 
335
 
 
336
ReturnedValue FunctionPrototype::method_bind(CallContext *ctx)
 
337
{
 
338
    Scope scope(ctx);
 
339
    Scoped<FunctionObject> target(scope, ctx->callData->thisObject);
 
340
    if (!target)
 
341
        return ctx->throwTypeError();
 
342
 
 
343
    ScopedValue boundThis(scope, ctx->argument(0));
 
344
    QVector<Value> boundArgs;
 
345
    for (int i = 1; i < ctx->callData->argc; ++i)
 
346
        boundArgs += ctx->callData->args[i];
 
347
 
 
348
    return ctx->engine->newBoundFunction(ctx->engine->rootContext, target, boundThis, boundArgs)->asReturnedValue();
 
349
}
 
350
 
 
351
DEFINE_OBJECT_VTABLE(ScriptFunction);
 
352
 
 
353
ScriptFunction::ScriptFunction(ExecutionContext *scope, Function *function)
 
354
    : SimpleScriptFunction(scope, function, true)
 
355
{
 
356
    setVTable(staticVTable());
 
357
 
 
358
    Scope s(scope);
 
359
    ScopedValue protectThis(s, this);
 
360
 
 
361
    // global function
 
362
    if (!scope)
 
363
        return;
 
364
 
 
365
    ExecutionEngine *v4 = scope->engine;
 
366
 
 
367
    needsActivation = function->needsActivation();
 
368
    strictMode = function->isStrict();
 
369
 
 
370
    defineReadonlyProperty(scope->engine->id_length, Primitive::fromInt32(formalParameterCount()));
 
371
 
 
372
    if (scope->strictMode) {
 
373
        Property pd(v4->thrower, v4->thrower);
 
374
        insertMember(scope->engine->id_caller, pd, Attr_Accessor|Attr_NotConfigurable|Attr_NotEnumerable);
 
375
        insertMember(scope->engine->id_arguments, pd, Attr_Accessor|Attr_NotConfigurable|Attr_NotEnumerable);
 
376
    }
 
377
}
 
378
 
 
379
ReturnedValue ScriptFunction::construct(Managed *that, CallData *callData)
 
380
{
 
381
    ExecutionEngine *v4 = that->internalClass->engine;
 
382
    if (v4->hasException)
 
383
        return Encode::undefined();
 
384
    CHECK_STACK_LIMITS(v4);
 
385
 
 
386
    Scope scope(v4);
 
387
    Scoped<ScriptFunction> f(scope, static_cast<ScriptFunction *>(that));
 
388
 
 
389
    InternalClass *ic = f->internalClassForConstructor();
 
390
    ScopedObject obj(scope, v4->newObject(ic));
 
391
 
 
392
    ExecutionContext *context = v4->currentContext();
 
393
    callData->thisObject = obj.asReturnedValue();
 
394
    ExecutionContext *ctx = context->newCallContext(f.getPointer(), callData);
 
395
 
 
396
    ExecutionContextSaver ctxSaver(context);
 
397
    ScopedValue result(scope, Q_V4_PROFILE(v4, ctx, f->function));
 
398
 
 
399
    if (f->function->compiledFunction->hasQmlDependencies())
 
400
        QmlContextWrapper::registerQmlDependencies(v4, f->function->compiledFunction);
 
401
 
 
402
    if (result->isObject())
 
403
        return result.asReturnedValue();
 
404
    return obj.asReturnedValue();
 
405
}
 
406
 
 
407
ReturnedValue ScriptFunction::call(Managed *that, CallData *callData)
 
408
{
 
409
    ScriptFunction *f = static_cast<ScriptFunction *>(that);
 
410
    ExecutionEngine *v4 = f->internalClass->engine;
 
411
    if (v4->hasException)
 
412
        return Encode::undefined();
 
413
    CHECK_STACK_LIMITS(v4);
 
414
 
 
415
    ExecutionContext *context = v4->currentContext();
 
416
    Scope scope(context);
 
417
 
 
418
    CallContext *ctx = context->newCallContext(f, callData);
 
419
 
 
420
    ExecutionContextSaver ctxSaver(context);
 
421
    ScopedValue result(scope, Q_V4_PROFILE(v4, ctx, f->function));
 
422
 
 
423
    if (f->function->compiledFunction->hasQmlDependencies())
 
424
        QmlContextWrapper::registerQmlDependencies(ctx->engine, f->function->compiledFunction);
 
425
 
 
426
    return result.asReturnedValue();
 
427
}
 
428
 
 
429
DEFINE_OBJECT_VTABLE(SimpleScriptFunction);
 
430
 
 
431
SimpleScriptFunction::SimpleScriptFunction(ExecutionContext *scope, Function *function, bool createProto)
 
432
    : FunctionObject(scope, function->name(), createProto)
 
433
{
 
434
    setVTable(staticVTable());
 
435
 
 
436
    Scope s(scope);
 
437
    ScopedValue protectThis(s, this);
 
438
 
 
439
    this->function = function;
 
440
    this->function->compilationUnit->ref();
 
441
    Q_ASSERT(function);
 
442
    Q_ASSERT(function->code);
 
443
 
 
444
    // global function
 
445
    if (!scope)
 
446
        return;
 
447
 
 
448
    ExecutionEngine *v4 = scope->engine;
 
449
 
 
450
    needsActivation = function->needsActivation();
 
451
    strictMode = function->isStrict();
 
452
 
 
453
    defineReadonlyProperty(scope->engine->id_length, Primitive::fromInt32(formalParameterCount()));
 
454
 
 
455
    if (scope->strictMode) {
 
456
        Property pd(v4->thrower, v4->thrower);
 
457
        insertMember(scope->engine->id_caller, pd, Attr_Accessor|Attr_NotConfigurable|Attr_NotEnumerable);
 
458
        insertMember(scope->engine->id_arguments, pd, Attr_Accessor|Attr_NotConfigurable|Attr_NotEnumerable);
 
459
    }
 
460
}
 
461
 
 
462
ReturnedValue SimpleScriptFunction::construct(Managed *that, CallData *callData)
 
463
{
 
464
    ExecutionEngine *v4 = that->internalClass->engine;
 
465
    if (v4->hasException)
 
466
        return Encode::undefined();
 
467
    CHECK_STACK_LIMITS(v4);
 
468
 
 
469
    Scope scope(v4);
 
470
    Scoped<SimpleScriptFunction> f(scope, static_cast<SimpleScriptFunction *>(that));
 
471
 
 
472
    InternalClass *ic = f->internalClassForConstructor();
 
473
    callData->thisObject = v4->newObject(ic);
 
474
 
 
475
    ExecutionContext *context = v4->currentContext();
 
476
    ExecutionContextSaver ctxSaver(context);
 
477
 
 
478
    CallContext ctx(v4);
 
479
    ctx.strictMode = f->strictMode;
 
480
    ctx.callData = callData;
 
481
    ctx.function = f.getPointer();
 
482
    ctx.compilationUnit = f->function->compilationUnit;
 
483
    ctx.lookups = ctx.compilationUnit->runtimeLookups;
 
484
    ctx.outer = f->scope;
 
485
    ctx.locals = v4->stackPush(f->varCount());
 
486
    while (callData->argc < (int)f->formalParameterCount()) {
 
487
        callData->args[callData->argc] = Encode::undefined();
 
488
        ++callData->argc;
 
489
    }
 
490
    Q_ASSERT(v4->currentContext() == &ctx);
 
491
 
 
492
    Scoped<Object> result(scope, Q_V4_PROFILE(v4, &ctx, f->function));
 
493
 
 
494
    if (f->function->compiledFunction->hasQmlDependencies())
 
495
        QmlContextWrapper::registerQmlDependencies(v4, f->function->compiledFunction);
 
496
 
 
497
    if (!result)
 
498
        return callData->thisObject.asReturnedValue();
 
499
    return result.asReturnedValue();
 
500
}
 
501
 
 
502
ReturnedValue SimpleScriptFunction::call(Managed *that, CallData *callData)
 
503
{
 
504
    ExecutionEngine *v4 = that->internalClass->engine;
 
505
    if (v4->hasException)
 
506
        return Encode::undefined();
 
507
    CHECK_STACK_LIMITS(v4);
 
508
 
 
509
    SimpleScriptFunction *f = static_cast<SimpleScriptFunction *>(that);
 
510
 
 
511
    Scope scope(v4);
 
512
    ExecutionContext *context = v4->currentContext();
 
513
    ExecutionContextSaver ctxSaver(context);
 
514
 
 
515
    CallContext ctx(v4);
 
516
    ctx.strictMode = f->strictMode;
 
517
    ctx.callData = callData;
 
518
    ctx.function = f;
 
519
    ctx.compilationUnit = f->function->compilationUnit;
 
520
    ctx.lookups = ctx.compilationUnit->runtimeLookups;
 
521
    ctx.outer = f->scope;
 
522
    ctx.locals = v4->stackPush(f->varCount());
 
523
    while (callData->argc < (int)f->formalParameterCount()) {
 
524
        callData->args[callData->argc] = Encode::undefined();
 
525
        ++callData->argc;
 
526
    }
 
527
    Q_ASSERT(v4->currentContext() == &ctx);
 
528
 
 
529
    ScopedValue result(scope, Q_V4_PROFILE(v4, &ctx, f->function));
 
530
 
 
531
    if (f->function->compiledFunction->hasQmlDependencies())
 
532
        QmlContextWrapper::registerQmlDependencies(v4, f->function->compiledFunction);
 
533
 
 
534
    return result.asReturnedValue();
 
535
}
 
536
 
 
537
InternalClass *SimpleScriptFunction::internalClassForConstructor()
 
538
{
 
539
    ReturnedValue proto = protoProperty();
 
540
    InternalClass *classForConstructor;
 
541
    Scope scope(internalClass->engine);
 
542
    ScopedObject p(scope, proto);
 
543
    if (p)
 
544
        classForConstructor = internalClass->engine->constructClass->changePrototype(p.getPointer());
 
545
    else
 
546
        classForConstructor = scope.engine->objectClass;
 
547
 
 
548
    return classForConstructor;
 
549
}
 
550
 
 
551
 
 
552
 
 
553
DEFINE_OBJECT_VTABLE(BuiltinFunction);
 
554
 
 
555
BuiltinFunction::BuiltinFunction(ExecutionContext *scope, const StringRef name, ReturnedValue (*code)(CallContext *))
 
556
    : FunctionObject(scope, name)
 
557
    , code(code)
 
558
{
 
559
    setVTable(staticVTable());
 
560
}
 
561
 
 
562
ReturnedValue BuiltinFunction::construct(Managed *f, CallData *)
 
563
{
 
564
    return f->internalClass->engine->currentContext()->throwTypeError();
 
565
}
 
566
 
 
567
ReturnedValue BuiltinFunction::call(Managed *that, CallData *callData)
 
568
{
 
569
    BuiltinFunction *f = static_cast<BuiltinFunction *>(that);
 
570
    ExecutionEngine *v4 = f->internalClass->engine;
 
571
    if (v4->hasException)
 
572
        return Encode::undefined();
 
573
    CHECK_STACK_LIMITS(v4);
 
574
 
 
575
    ExecutionContext *context = v4->currentContext();
 
576
    ExecutionContextSaver ctxSaver(context);
 
577
 
 
578
    CallContext ctx(v4);
 
579
    ctx.strictMode = f->scope->strictMode; // ### needed? scope or parent context?
 
580
    ctx.callData = callData;
 
581
    Q_ASSERT(v4->currentContext() == &ctx);
 
582
 
 
583
    return f->code(&ctx);
 
584
}
 
585
 
 
586
ReturnedValue IndexedBuiltinFunction::call(Managed *that, CallData *callData)
 
587
{
 
588
    IndexedBuiltinFunction *f = static_cast<IndexedBuiltinFunction *>(that);
 
589
    ExecutionEngine *v4 = f->internalClass->engine;
 
590
    if (v4->hasException)
 
591
        return Encode::undefined();
 
592
    CHECK_STACK_LIMITS(v4);
 
593
 
 
594
    ExecutionContext *context = v4->currentContext();
 
595
    ExecutionContextSaver ctxSaver(context);
 
596
 
 
597
    CallContext ctx(v4);
 
598
    ctx.strictMode = f->scope->strictMode; // ### needed? scope or parent context?
 
599
    ctx.callData = callData;
 
600
    Q_ASSERT(v4->currentContext() == &ctx);
 
601
 
 
602
    return f->code(&ctx, f->index);
 
603
}
 
604
 
 
605
DEFINE_OBJECT_VTABLE(IndexedBuiltinFunction);
 
606
 
 
607
DEFINE_OBJECT_VTABLE(BoundFunction);
 
608
 
 
609
BoundFunction::BoundFunction(ExecutionContext *scope, FunctionObjectRef target, const ValueRef boundThis, const QVector<Value> &boundArgs)
 
610
    : FunctionObject(scope, QStringLiteral("__bound function__"))
 
611
    , target(target)
 
612
    , boundArgs(boundArgs)
 
613
{
 
614
    setVTable(staticVTable());
 
615
    subtype = FunctionObject::BoundFunction;
 
616
    this->boundThis = boundThis;
 
617
 
 
618
    Scope s(scope);
 
619
    ScopedValue protectThis(s, this);
 
620
 
 
621
    ScopedValue l(s, target->get(scope->engine->id_length));
 
622
    int len = l->toUInt32();
 
623
    len -= boundArgs.size();
 
624
    if (len < 0)
 
625
        len = 0;
 
626
    defineReadonlyProperty(scope->engine->id_length, Primitive::fromInt32(len));
 
627
 
 
628
    ExecutionEngine *v4 = scope->engine;
 
629
 
 
630
    Property pd(v4->thrower, v4->thrower);
 
631
    insertMember(scope->engine->id_arguments, pd, Attr_Accessor|Attr_NotConfigurable|Attr_NotEnumerable);
 
632
    insertMember(scope->engine->id_caller, pd, Attr_Accessor|Attr_NotConfigurable|Attr_NotEnumerable);
 
633
}
 
634
 
 
635
void BoundFunction::destroy(Managed *that)
 
636
{
 
637
    static_cast<BoundFunction *>(that)->~BoundFunction();
 
638
}
 
639
 
 
640
ReturnedValue BoundFunction::call(Managed *that, CallData *dd)
 
641
{
 
642
    BoundFunction *f = static_cast<BoundFunction *>(that);
 
643
    Scope scope(f->scope->engine);
 
644
    if (scope.hasException())
 
645
        return Encode::undefined();
 
646
 
 
647
    ScopedCallData callData(scope, f->boundArgs.size() + dd->argc);
 
648
    callData->thisObject = f->boundThis;
 
649
    memcpy(callData->args, f->boundArgs.constData(), f->boundArgs.size()*sizeof(Value));
 
650
    memcpy(callData->args + f->boundArgs.size(), dd->args, dd->argc*sizeof(Value));
 
651
    return f->target->call(callData);
 
652
}
 
653
 
 
654
ReturnedValue BoundFunction::construct(Managed *that, CallData *dd)
 
655
{
 
656
    BoundFunction *f = static_cast<BoundFunction *>(that);
 
657
    Scope scope(f->scope->engine);
 
658
    if (scope.hasException())
 
659
        return Encode::undefined();
 
660
 
 
661
    ScopedCallData callData(scope, f->boundArgs.size() + dd->argc);
 
662
    memcpy(callData->args, f->boundArgs.constData(), f->boundArgs.size()*sizeof(Value));
 
663
    memcpy(callData->args + f->boundArgs.size(), dd->args, dd->argc*sizeof(Value));
 
664
    return f->target->construct(callData);
 
665
}
 
666
 
 
667
void BoundFunction::markObjects(Managed *that, ExecutionEngine *e)
 
668
{
 
669
    BoundFunction *o = static_cast<BoundFunction *>(that);
 
670
    o->target->mark(e);
 
671
    o->boundThis.mark(e);
 
672
    for (int i = 0; i < o->boundArgs.size(); ++i)
 
673
        o->boundArgs.at(i).mark(e);
 
674
    FunctionObject::markObjects(that, e);
 
675
}