Home | History | Annotate | Download | only in api
      1 /*
      2     Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies)
      3 
      4     This library is free software; you can redistribute it and/or
      5     modify it under the terms of the GNU Library General Public
      6     License as published by the Free Software Foundation; either
      7     version 2 of the License, or (at your option) any later version.
      8 
      9     This library is distributed in the hope that it will be useful,
     10     but WITHOUT ANY WARRANTY; without even the implied warranty of
     11     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
     12     Library General Public License for more details.
     13 
     14     You should have received a copy of the GNU Library General Public License
     15     along with this library; see the file COPYING.LIB.  If not, write to
     16     the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
     17     Boston, MA 02110-1301, USA.
     18 */
     19 
     20 #include "config.h"
     21 
     22 #include "qscriptengine_p.h"
     23 
     24 #include "qscriptfunction_p.h"
     25 #include "qscriptprogram_p.h"
     26 #include "qscriptvalue_p.h"
     27 
     28 /*!
     29     Constructs a default QScriptEnginePrivate object, a new global context will be created.
     30     \internal
     31 */
     32 QScriptEnginePrivate::QScriptEnginePrivate(const QScriptEngine* engine)
     33     : q_ptr(const_cast<QScriptEngine*>(engine))
     34     , m_context(JSGlobalContextCreate(0))
     35     , m_exception(0)
     36     , m_originalGlobalObject(m_context)
     37     , m_nativeFunctionClass(JSClassCreate(&qt_NativeFunctionClass))
     38     , m_nativeFunctionWithArgClass(JSClassCreate(&qt_NativeFunctionWithArgClass))
     39 {
     40 }
     41 
     42 QScriptEnginePrivate::~QScriptEnginePrivate()
     43 {
     44     JSClassRelease(m_nativeFunctionClass);
     45     JSClassRelease(m_nativeFunctionWithArgClass);
     46     if (m_exception)
     47         JSValueUnprotect(m_context, m_exception);
     48     JSGlobalContextRelease(m_context);
     49 }
     50 
     51 QScriptSyntaxCheckResultPrivate* QScriptEnginePrivate::checkSyntax(const QString& program)
     52 {
     53     JSValueRef exception;
     54     JSStringRef source = QScriptConverter::toString(program);
     55     bool syntaxIsCorrect = JSCheckScriptSyntax(m_context, source, /* url */ 0, /* starting line */ 1, &exception);
     56     JSStringRelease(source);
     57     if (syntaxIsCorrect) {
     58         return new QScriptSyntaxCheckResultPrivate(this);
     59     }
     60     JSValueProtect(m_context, exception);
     61     return new QScriptSyntaxCheckResultPrivate(this, const_cast<JSObjectRef>(exception));
     62 }
     63 
     64 /*!
     65     Evaluates program and returns the result of the evaluation.
     66     \internal
     67 */
     68 QScriptValuePrivate* QScriptEnginePrivate::evaluate(const QString& program, const QString& fileName, int lineNumber)
     69 {
     70     JSStringRef script = QScriptConverter::toString(program);
     71     JSStringRef file = QScriptConverter::toString(fileName);
     72     QScriptValuePrivate* result = new QScriptValuePrivate(this, evaluate(script, file, lineNumber));
     73     JSStringRelease(script);
     74     JSStringRelease(file);
     75     return result;
     76 }
     77 
     78 /*!
     79     Evaluates program and returns the result of the evaluation.
     80     \internal
     81 */
     82 QScriptValuePrivate* QScriptEnginePrivate::evaluate(const QScriptProgramPrivate* program)
     83 {
     84     if (program->isNull())
     85         return new QScriptValuePrivate;
     86     return new QScriptValuePrivate(this, evaluate(*program, program->file(), program->line()));
     87 }
     88 
     89 QScriptValuePrivate* QScriptEnginePrivate::uncaughtException() const
     90 {
     91     return m_exception ? new QScriptValuePrivate(this, m_exception) : new QScriptValuePrivate();
     92 }
     93 
     94 QScriptValuePrivate* QScriptEnginePrivate::newFunction(QScriptEngine::FunctionSignature fun, QScriptValuePrivate* prototype, int length)
     95 {
     96     // Note that this private data will be deleted in the object finalize function.
     97     QNativeFunctionData* data = new QNativeFunctionData(this, fun);
     98     JSObjectRef funJS = JSObjectMake(m_context, m_nativeFunctionClass, reinterpret_cast<void*>(data));
     99     QScriptValuePrivate* proto = prototype ? prototype : newObject();
    100     return newFunction(funJS, proto);
    101 }
    102 
    103 QScriptValuePrivate* QScriptEnginePrivate::newFunction(QScriptEngine::FunctionWithArgSignature fun, void* arg)
    104 {
    105     // Note that this private data will be deleted in the object finalize function.
    106     QNativeFunctionWithArgData* data = new QNativeFunctionWithArgData(this, fun, arg);
    107     JSObjectRef funJS = JSObjectMake(m_context, m_nativeFunctionWithArgClass, reinterpret_cast<void*>(data));
    108     QScriptValuePrivate* proto = newObject();
    109     return newFunction(funJS, proto);
    110 }
    111 
    112 QScriptValuePrivate* QScriptEnginePrivate::newFunction(JSObjectRef funJS, QScriptValuePrivate* prototype)
    113 {
    114     JSObjectSetPrototype(m_context, funJS, m_originalGlobalObject.functionPrototype());
    115 
    116     QScriptValuePrivate* result = new QScriptValuePrivate(this, funJS);
    117     static JSStringRef protoName = QScriptConverter::toString("prototype");
    118     static JSStringRef constructorName = QScriptConverter::toString("constructor");
    119     result->setProperty(protoName, prototype, QScriptValue::Undeletable);
    120     prototype->setProperty(constructorName, result, QScriptValue::PropertyFlags(QScriptValue::Undeletable | QScriptValue::SkipInEnumeration));
    121 
    122     return result;
    123 }
    124 
    125 QScriptValuePrivate* QScriptEnginePrivate::newObject() const
    126 {
    127     return new QScriptValuePrivate(this, JSObjectMake(m_context, /* jsClass */ 0, /* userData */ 0));
    128 }
    129 
    130 QScriptValuePrivate* QScriptEnginePrivate::newArray(uint length)
    131 {
    132     JSValueRef exception = 0;
    133     JSObjectRef array = JSObjectMakeArray(m_context, /* argumentCount */ 0, /* arguments */ 0, &exception);
    134 
    135     if (!exception) {
    136         if (length > 0) {
    137             JSRetainPtr<JSStringRef> lengthRef(Adopt, JSStringCreateWithUTF8CString("length"));
    138             // array is an Array instance, so an exception should not occure here.
    139             JSObjectSetProperty(m_context, array, lengthRef.get(), JSValueMakeNumber(m_context, length), kJSPropertyAttributeNone, /* exception */ 0);
    140         }
    141     } else {
    142         setException(exception, NotNullException);
    143         return new QScriptValuePrivate();
    144     }
    145 
    146     return new QScriptValuePrivate(this, array);
    147 }
    148 
    149 QScriptValuePrivate* QScriptEnginePrivate::newDate(qsreal value)
    150 {
    151     JSValueRef exception = 0;
    152     JSValueRef argument = JSValueMakeNumber(m_context, value);
    153     JSObjectRef result = JSObjectMakeDate(m_context, /* argumentCount */ 1, &argument, &exception);
    154 
    155     if (exception) {
    156         setException(exception, NotNullException);
    157         return new QScriptValuePrivate();
    158     }
    159 
    160     return new QScriptValuePrivate(this, result);
    161 }
    162 
    163 QScriptValuePrivate* QScriptEnginePrivate::globalObject() const
    164 {
    165     JSObjectRef globalObject = JSContextGetGlobalObject(m_context);
    166     return new QScriptValuePrivate(this, globalObject);
    167 }
    168