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

« 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-26 09:12:59 UTC
  • Revision ID: package-import@ubuntu.com-20150226091259-krq068zh9bwi20or
Tags: 5.4.0-0ubuntu2
Sync package with qtdeclarative-opensource-src - 5.4.0-4ubuntu2

Show diffs side-by-side

added added

removed removed

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