src/script/api/qscriptengine.cpp
changeset 22 79de32ba3296
parent 19 fcece45ef507
child 30 5dc02b23752f
equal deleted inserted replaced
19:fcece45ef507 22:79de32ba3296
    39 #include "qdebug.h"
    39 #include "qdebug.h"
    40 
    40 
    41 #include <QtCore/qstringlist.h>
    41 #include <QtCore/qstringlist.h>
    42 #include <QtCore/qmetaobject.h>
    42 #include <QtCore/qmetaobject.h>
    43 
    43 
       
    44 #include "CodeBlock.h"
    44 #include "Error.h"
    45 #include "Error.h"
    45 #include "JSArray.h"
    46 #include "JSArray.h"
    46 #include "JSLock.h"
    47 #include "JSLock.h"
    47 #include "Interpreter.h"
    48 #include "Interpreter.h"
    48 #include "DateConstructor.h"
    49 #include "DateConstructor.h"
   660         if (encStr == QLatin1String("CodecForTr"))
   661         if (encStr == QLatin1String("CodecForTr"))
   661             encoding = QCoreApplication::CodecForTr;
   662             encoding = QCoreApplication::CodecForTr;
   662         else if (encStr == QLatin1String("UnicodeUTF8"))
   663         else if (encStr == QLatin1String("UnicodeUTF8"))
   663             encoding = QCoreApplication::UnicodeUTF8;
   664             encoding = QCoreApplication::UnicodeUTF8;
   664         else
   665         else
   665             return JSC::throwError(exec, JSC::GeneralError, QString::fromLatin1("qsTranslate(): invalid encoding '%s'").arg(encStr));
   666             return JSC::throwError(exec, JSC::GeneralError, QString::fromLatin1("qsTranslate(): invalid encoding '%0'").arg(encStr));
   666     }
   667     }
   667     int n = -1;
   668     int n = -1;
   668     if (args.size() > 4)
   669     if (args.size() > 4)
   669         n = args.at(4).toInt32(exec);
   670         n = args.at(4).toInt32(exec);
   670 #endif
   671 #endif
   694     if (!args.at(0).isString())
   695     if (!args.at(0).isString())
   695         return JSC::throwError(exec, JSC::GeneralError, "qsTr(): first argument (text) must be a string");
   696         return JSC::throwError(exec, JSC::GeneralError, "qsTr(): first argument (text) must be a string");
   696     if ((args.size() > 1) && !args.at(1).isString())
   697     if ((args.size() > 1) && !args.at(1).isString())
   697         return JSC::throwError(exec, JSC::GeneralError, "qsTr(): second argument (comment) must be a string");
   698         return JSC::throwError(exec, JSC::GeneralError, "qsTr(): second argument (comment) must be a string");
   698     if ((args.size() > 2) && !args.at(2).isNumber())
   699     if ((args.size() > 2) && !args.at(2).isNumber())
   699         return JSC::throwError(exec, JSC::GeneralError, "qsTranslate(): third argument (n) must be a number");
   700         return JSC::throwError(exec, JSC::GeneralError, "qsTr(): third argument (n) must be a number");
   700 #ifndef QT_NO_QOBJECT
   701 #ifndef QT_NO_QOBJECT
   701     QString context;
   702     QString context;
   702     QScriptContext *ctx = QScriptEnginePrivate::contextForFrame(exec);
   703     // The first non-empty source URL in the call stack determines the translation context.
   703     if (ctx && ctx->parentContext())
   704     {
   704         context = QFileInfo(QScriptContextInfo(ctx->parentContext()).fileName()).baseName();
   705         JSC::ExecState *frame = exec->removeHostCallFrameFlag();
       
   706         while (frame) {
       
   707             if (frame->codeBlock() && frame->codeBlock()->source()
       
   708                 && !frame->codeBlock()->source()->url().isEmpty()) {
       
   709                 context = QFileInfo(frame->codeBlock()->source()->url()).baseName();
       
   710                 break;
       
   711             }
       
   712             frame = frame->callerFrame()->removeHostCallFrameFlag();
       
   713         }
       
   714     }
   705 #endif
   715 #endif
   706     QString text(args.at(0).toString(exec));
   716     QString text(args.at(0).toString(exec));
   707 #ifndef QT_NO_QOBJECT
   717 #ifndef QT_NO_QOBJECT
   708     QString comment;
   718     QString comment;
   709     if (args.size() > 1)
   719     if (args.size() > 1)
  1005 void QScriptEnginePrivate::setGlobalObject(JSC::JSObject *object)
  1015 void QScriptEnginePrivate::setGlobalObject(JSC::JSObject *object)
  1006 {
  1016 {
  1007     if (object == globalObject())
  1017     if (object == globalObject())
  1008         return;
  1018         return;
  1009     QScript::GlobalObject *glob = static_cast<QScript::GlobalObject*>(originalGlobalObject());
  1019     QScript::GlobalObject *glob = static_cast<QScript::GlobalObject*>(originalGlobalObject());
  1010     if (object == originalGlobalObjectProxy)
  1020     if (object == originalGlobalObjectProxy) {
  1011         glob->customGlobalObject = 0;
  1021         glob->customGlobalObject = 0;
  1012     else {
  1022         // Sync the internal prototype, since JSObject::prototype() is not virtual.
       
  1023         glob->setPrototype(originalGlobalObjectProxy->prototype());
       
  1024     } else {
  1013         Q_ASSERT(object != originalGlobalObject());
  1025         Q_ASSERT(object != originalGlobalObject());
  1014         glob->customGlobalObject = object;
  1026         glob->customGlobalObject = object;
       
  1027         // Sync the internal prototype, since JSObject::prototype() is not virtual.
       
  1028         glob->setPrototype(object->prototype());
  1015     }
  1029     }
  1016 }
  1030 }
  1017 
  1031 
  1018 /*!
  1032 /*!
  1019   \internal
  1033   \internal
  1587     return result;
  1601     return result;
  1588 }
  1602 }
  1589 
  1603 
  1590 #ifndef QT_NO_REGEXP
  1604 #ifndef QT_NO_REGEXP
  1591 
  1605 
  1592 extern QString qt_regexp_toCanonical(const QString &, QRegExp::PatternSyntax);
  1606 Q_DECL_IMPORT extern QString qt_regexp_toCanonical(const QString &, QRegExp::PatternSyntax);
  1593 
  1607 
  1594 /*!
  1608 /*!
  1595   Creates a QtScript object of class RegExp with the given
  1609   Creates a QtScript object of class RegExp with the given
  1596   \a regexp.
  1610   \a regexp.
  1597 
  1611 
  2923     if (!jscObject || !jscObject.isObject())
  2937     if (!jscObject || !jscObject.isObject())
  2924         jscObject = d->globalObject();
  2938         jscObject = d->globalObject();
  2925 //    unsigned attribs = JSC::DontEnum;
  2939 //    unsigned attribs = JSC::DontEnum;
  2926     JSC::asObject(jscObject)->putDirectFunction(exec, new (exec)JSC::NativeFunctionWrapper(exec, glob->prototypeFunctionStructure(), 5, JSC::Identifier(exec, "qsTranslate"), QScript::functionQsTranslate));
  2940     JSC::asObject(jscObject)->putDirectFunction(exec, new (exec)JSC::NativeFunctionWrapper(exec, glob->prototypeFunctionStructure(), 5, JSC::Identifier(exec, "qsTranslate"), QScript::functionQsTranslate));
  2927     JSC::asObject(jscObject)->putDirectFunction(exec, new (exec)JSC::NativeFunctionWrapper(exec, glob->prototypeFunctionStructure(), 2, JSC::Identifier(exec, "QT_TRANSLATE_NOOP"), QScript::functionQsTranslateNoOp));
  2941     JSC::asObject(jscObject)->putDirectFunction(exec, new (exec)JSC::NativeFunctionWrapper(exec, glob->prototypeFunctionStructure(), 2, JSC::Identifier(exec, "QT_TRANSLATE_NOOP"), QScript::functionQsTranslateNoOp));
  2928     JSC::asObject(jscObject)->putDirectFunction(exec, new (exec)JSC::NativeFunctionWrapper(exec, glob->prototypeFunctionStructure(), 3, JSC::Identifier(exec, "qsTr"), QScript::functionQsTr));
  2942     JSC::asObject(jscObject)->putDirectFunction(exec, new (exec)JSC::PrototypeFunction(exec, glob->prototypeFunctionStructure(), 3, JSC::Identifier(exec, "qsTr"), QScript::functionQsTr));
  2929     JSC::asObject(jscObject)->putDirectFunction(exec, new (exec)JSC::NativeFunctionWrapper(exec, glob->prototypeFunctionStructure(), 1, JSC::Identifier(exec, "QT_TR_NOOP"), QScript::functionQsTrNoOp));
  2943     JSC::asObject(jscObject)->putDirectFunction(exec, new (exec)JSC::NativeFunctionWrapper(exec, glob->prototypeFunctionStructure(), 1, JSC::Identifier(exec, "QT_TR_NOOP"), QScript::functionQsTrNoOp));
  2930 
  2944 
  2931     glob->stringPrototype()->putDirectFunction(exec, new (exec)JSC::NativeFunctionWrapper(exec, glob->prototypeFunctionStructure(), 1, JSC::Identifier(exec, "arg"), QScript::stringProtoFuncArg));
  2945     glob->stringPrototype()->putDirectFunction(exec, new (exec)JSC::NativeFunctionWrapper(exec, glob->prototypeFunctionStructure(), 1, JSC::Identifier(exec, "arg"), QScript::stringProtoFuncArg));
  2932 }
  2946 }
  2933 
  2947