2 * Copyright (c) 2011 Samsung Electronics Co., Ltd All Rights Reserved
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
17 * @file js_overlay_support.cpp
23 #include <dpl/log/log.h>
24 #include <dpl/foreach.h>
25 #include <JavaScriptCore/JavaScript.h>
27 #include <js_overlay_support.h>
28 #include <CommonsJavaScript/JSLifeManager.h>
30 namespace JSOverlaySupport {
31 class JSFunctionDispatcher
36 //required to unprotect object in destructor;
38 JSObjectPtr originalFunction;
39 JSObjectPtr overlayFunction;
42 static JSClassRef getClassRef();
45 static JSClassDefinition m_classInfo;
48 static void initialize(JSContextRef context, JSObjectRef object);
50 static void finalize(JSObjectRef object);
52 static JSValueRef callAsFunction(JSContextRef ctx,
54 JSObjectRef thisObject,
56 const JSValueRef arguments[],
57 JSValueRef* exception);
60 JSObjectPtr createWrappedFunction(
61 JSGlobalContextRef ctx,
62 const JSObjectPtr& originalFunction,
63 const JSObjectPtr& overlayFunction,
64 const std::string& name)
66 LogDebug("Creation overlay for function: " << name);
67 JSStringRef name_js = JSStringCreateWithUTF8CString(name.c_str());
69 JSFunctionDispatcher::PrivateData* priv =
70 new JSFunctionDispatcher::PrivateData;
72 priv->overlayFunction = overlayFunction;
73 priv->originalFunction = originalFunction;
75 auto fun = JSObjectMake(ctx, JSFunctionDispatcher::getClassRef(), priv);
77 JSStringRelease(name_js);
79 LogDebug("JSValueSafeProtect invoked for: "
80 << overlayFunction->getObject() << " "
81 << originalFunction->getObject() << " Wrapper:"
84 //the value is unprotected in finalize of the JSFunctionDispatcher object
85 JSValueSafeProtect(ctx,
86 static_cast<JSObjectRef>(
87 overlayFunction->getObject()));
89 return JSObjectPtr(new JSObject(fun));
92 JSClassDefinition JSFunctionDispatcher::m_classInfo = {
94 kJSClassAttributeNone,
112 JSClassRef JSFunctionDispatcher::getClassRef()
114 static auto classRef = JSClassCreate(&m_classInfo);
118 void JSFunctionDispatcher::initialize(JSContextRef /*context*/,
119 JSObjectRef /*object*/)
121 LogDebug("Initialize");
124 void JSFunctionDispatcher::finalize(JSObjectRef object)
126 LogDebug("finalize");
128 PrivateData* priv = static_cast<PrivateData*>(JSObjectGetPrivate(object));
130 JSValueSafeUnprotect(priv->context,
131 static_cast<JSObjectRef>(
132 priv->overlayFunction->getObject()));
137 JSValueRef JSFunctionDispatcher::callAsFunction(
138 JSContextRef context,
140 JSObjectRef thisObject,
141 size_t argumentCount,
142 const JSValueRef arguments[],
143 JSValueRef* exception)
145 LogDebug("Dispatcher invoked");
147 JSValueRef result = JSValueMakeUndefined(context);
149 JSFunctionDispatcher::PrivateData* priv =
150 static_cast<JSFunctionDispatcher::PrivateData*>
151 (JSObjectGetPrivate(object));
154 LogError("Private object is NULL");
158 //call overlayed function
159 if (priv->overlayFunction->getObject()) {
160 LogDebug("Overlayed function will be invoked...");
161 result = JSObjectCallAsFunction(
163 static_cast<JSObjectRef>(
164 priv->overlayFunction->getObject()),
171 //call original function
172 if (priv->originalFunction->getObject()) {
173 LogDebug("Original function will be invoked..");
174 result = JSObjectCallAsFunction(
176 static_cast<JSObjectRef>(
177 priv->originalFunction->getObject()),