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>
29 namespace JSOverlaySupport
32 class JSFunctionDispatcher
37 //required to unprotect object in destructor;
39 JSObjectPtr originalFunction;
40 JSObjectPtr overlayFunction;
43 static JSClassRef getClassRef();
46 static JSClassDefinition m_classInfo;
49 static void initialize(JSContextRef context, JSObjectRef object);
51 static void finalize(JSObjectRef object);
53 static JSValueRef callAsFunction(JSContextRef ctx,
55 JSObjectRef thisObject,
57 const JSValueRef arguments[],
58 JSValueRef* exception);
63 JSObjectPtr createWrappedFunction(
64 JSGlobalContextRef ctx,
65 const JSObjectPtr& originalFunction,
66 const JSObjectPtr& overlayFunction,
67 const std::string& name)
69 LogDebug("Creation overlay for function: " << name);
70 JSStringRef name_js = JSStringCreateWithUTF8CString(name.c_str());
72 JSFunctionDispatcher::PrivateData* priv =
73 new JSFunctionDispatcher::PrivateData;
75 priv->overlayFunction = overlayFunction;
76 priv->originalFunction = originalFunction;
78 auto fun = JSObjectMake(ctx, JSFunctionDispatcher::getClassRef(), priv);
80 JSStringRelease(name_js);
82 LogDebug("JSValueProtect invoked for: "
83 << overlayFunction->getObject() << " "
84 << originalFunction->getObject() << " Wrapper:"
87 //the value is unprotected in finalize of the JSFunctionDispatcher object
89 static_cast<JSObjectRef>(
90 overlayFunction->getObject()));
92 return JSObjectPtr(new JSObject(fun));
97 JSClassDefinition JSFunctionDispatcher::m_classInfo = {
99 kJSClassAttributeNone,
117 JSClassRef JSFunctionDispatcher::getClassRef()
119 static auto classRef = JSClassCreate(&m_classInfo);
124 void JSFunctionDispatcher::initialize(JSContextRef /*context*/,
125 JSObjectRef /*object*/)
127 LogDebug("Initialize");
130 void JSFunctionDispatcher::finalize(JSObjectRef object)
132 LogDebug("finalize");
134 PrivateData* priv = static_cast<PrivateData*> (JSObjectGetPrivate(object));
137 JSValueUnprotect(priv->context,
138 static_cast<JSObjectRef>(
139 priv->overlayFunction->getObject()));
146 JSValueRef JSFunctionDispatcher::callAsFunction(
147 JSContextRef context,
149 JSObjectRef thisObject,
150 size_t argumentCount,
151 const JSValueRef arguments[],
152 JSValueRef* exception)
154 LogDebug("Dispatcher invoked");
156 JSValueRef result = JSValueMakeUndefined(context);
158 JSFunctionDispatcher::PrivateData* priv =
159 static_cast<JSFunctionDispatcher::PrivateData*>
160 (JSObjectGetPrivate(object));
164 LogError("Private object is NULL");
168 //call overlayed function
169 if (priv->overlayFunction->getObject())
171 LogDebug("Overlayed function will be invoked...");
172 result = JSObjectCallAsFunction(
174 static_cast<JSObjectRef>(
175 priv->overlayFunction->getObject()),
182 //call original function
183 if (priv->originalFunction->getObject())
185 LogDebug("Original function will be invoked..");
186 result = JSObjectCallAsFunction(
188 static_cast<JSObjectRef>(
189 priv->originalFunction->getObject()),