2 Copyright (C) 2010 Nokia Corporation and/or its subsidiary(-ies)
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.
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.
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.
20 // Contains the necessary helper structures to make possible expose
21 // C/C++ functions to JavaScript environment.
25 #include "qscriptfunction_p.h"
27 static void qt_NativeFunction_finalize(JSObjectRef object)
29 void* priv = JSObjectGetPrivate(object);
30 delete reinterpret_cast<QNativeFunctionData*>(priv);
33 static JSValueRef qt_NativeFunction_callAsFunction(JSContextRef context, JSObjectRef object, JSObjectRef thisObject, size_t argumentCount, const JSValueRef arguments[], JSValueRef* exception)
35 QNativeFunctionData* data = reinterpret_cast<QNativeFunctionData*>(JSObjectGetPrivate(object));
37 // TODO: build a QScriptContext and use it in the native call.
38 QScriptContext* scriptContext = 0;
41 Q_UNUSED(argumentCount);
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);
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);
64 JSClassDefinition qt_NativeFunctionClass = {
66 kJSClassAttributeNoAutomaticPrototype, // attributes
75 qt_NativeFunction_finalize, // finalize
80 0, // getPropertyNames
81 qt_NativeFunction_callAsFunction, // callAsFunction
82 0, // callAsConstructor
87 static void qt_NativeFunctionWithArg_finalize(JSObjectRef object)
89 void* priv = JSObjectGetPrivate(object);
90 delete reinterpret_cast<QNativeFunctionWithArgData*>(priv);
93 static JSValueRef qt_NativeFunctionWithArg_callAsFunction(JSContextRef context, JSObjectRef object, JSObjectRef thisObject, size_t argumentCount, const JSValueRef arguments[], JSValueRef* exception)
95 QNativeFunctionWithArgData* data = reinterpret_cast<QNativeFunctionWithArgData*>(JSObjectGetPrivate(object));
97 // TODO: build a QScriptContext and use it in the native call.
98 QScriptContext* scriptContext = 0;
100 Q_UNUSED(thisObject);
101 Q_UNUSED(argumentCount);
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);
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);
124 JSClassDefinition qt_NativeFunctionWithArgClass = {
126 kJSClassAttributeNoAutomaticPrototype, // attributes
132 0, // staticFunctions
135 qt_NativeFunctionWithArg_finalize, // finalize
140 0, // getPropertyNames
141 qt_NativeFunctionWithArg_callAsFunction, // callAsFunction
142 0, // callAsConstructor