1 /* 2 Copyright (C) 2010 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 // Contains the necessary helper structures to make possible expose 21 // C/C++ functions to JavaScript environment. 22 23 #include "config.h" 24 25 #include "qscriptfunction_p.h" 26 27 static void qt_NativeFunction_finalize(JSObjectRef object) 28 { 29 void* priv = JSObjectGetPrivate(object); 30 delete reinterpret_cast<QNativeFunctionData*>(priv); 31 } 32 33 static JSValueRef qt_NativeFunction_callAsFunction(JSContextRef context, JSObjectRef object, JSObjectRef thisObject, size_t argumentCount, const JSValueRef arguments[], JSValueRef* exception) 34 { 35 QNativeFunctionData* data = reinterpret_cast<QNativeFunctionData*>(JSObjectGetPrivate(object)); 36 37 // TODO: build a QScriptContext and use it in the native call. 38 QScriptContext* scriptContext = 0; 39 Q_UNUSED(context); 40 Q_UNUSED(thisObject); 41 Q_UNUSED(argumentCount); 42 Q_UNUSED(arguments); 43 Q_UNUSED(exception); 44 45 QScriptEnginePrivate* engine = data->engine; 46 QScriptValuePrivate* result = QScriptValuePrivate::get(data->fun(scriptContext, QScriptEnginePrivate::get(engine))); 47 if (!result->isValid()) { 48 qWarning("Invalid value returned from native function, returning undefined value instead."); 49 return engine->makeJSValue(QScriptValue::UndefinedValue); 50 } 51 52 // Make sure that the result will be assigned to the correct engine. 53 if (!result->engine()) { 54 Q_ASSERT(result->isValid()); 55 result->assignEngine(engine); 56 } else if (result->engine() != engine) { 57 qWarning("Value from different engine returned from native function, returning undefined value instead."); 58 return engine->makeJSValue(QScriptValue::UndefinedValue); 59 } 60 61 return *result; 62 } 63 64 JSClassDefinition qt_NativeFunctionClass = { 65 0, // version 66 kJSClassAttributeNoAutomaticPrototype, // attributes 67 68 "", // className 69 0, // parentClass 70 71 0, // staticValues 72 0, // staticFunctions 73 74 0, // initialize 75 qt_NativeFunction_finalize, // finalize 76 0, // hasProperty 77 0, // getProperty 78 0, // setProperty 79 0, // deleteProperty 80 0, // getPropertyNames 81 qt_NativeFunction_callAsFunction, // callAsFunction 82 0, // callAsConstructor 83 0, // hasInstance 84 0 // convertToType 85 }; 86 87 static void qt_NativeFunctionWithArg_finalize(JSObjectRef object) 88 { 89 void* priv = JSObjectGetPrivate(object); 90 delete reinterpret_cast<QNativeFunctionWithArgData*>(priv); 91 } 92 93 static JSValueRef qt_NativeFunctionWithArg_callAsFunction(JSContextRef context, JSObjectRef object, JSObjectRef thisObject, size_t argumentCount, const JSValueRef arguments[], JSValueRef* exception) 94 { 95 QNativeFunctionWithArgData* data = reinterpret_cast<QNativeFunctionWithArgData*>(JSObjectGetPrivate(object)); 96 97 // TODO: build a QScriptContext and use it in the native call. 98 QScriptContext* scriptContext = 0; 99 Q_UNUSED(context); 100 Q_UNUSED(thisObject); 101 Q_UNUSED(argumentCount); 102 Q_UNUSED(arguments); 103 Q_UNUSED(exception); 104 105 QScriptEnginePrivate* engine = data->engine; 106 QScriptValuePrivate* result = QScriptValuePrivate::get(data->fun(scriptContext, QScriptEnginePrivate::get(engine), data->arg)); 107 if (!result->isValid()) { 108 qWarning("Invalid value returned from native function, returning undefined value instead."); 109 return engine->makeJSValue(QScriptValue::UndefinedValue); 110 } 111 112 // Make sure that the result will be assigned to the correct engine. 113 if (!result->engine()) { 114 Q_ASSERT(result->isValid()); 115 result->assignEngine(engine); 116 } else if (result->engine() != engine) { 117 qWarning("Value from different engine returned from native function, returning undefined value instead."); 118 return engine->makeJSValue(QScriptValue::UndefinedValue); 119 } 120 121 return *result; 122 } 123 124 JSClassDefinition qt_NativeFunctionWithArgClass = { 125 0, // version 126 kJSClassAttributeNoAutomaticPrototype, // attributes 127 128 "", // className 129 0, // parentClass 130 131 0, // staticValues 132 0, // staticFunctions 133 134 0, // initialize 135 qt_NativeFunctionWithArg_finalize, // finalize 136 0, // hasProperty 137 0, // getProperty 138 0, // setProperty 139 0, // deleteProperty 140 0, // getPropertyNames 141 qt_NativeFunctionWithArg_callAsFunction, // callAsFunction 142 0, // callAsConstructor 143 0, // hasInstance 144 0 // convertToType 145 }; 146