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 javascript_functions.cpp
18 * @author Grzegorz Krawczyk (g.krawczyk@samgsung.com)
23 #include <dpl/log/log.h>
24 #include <dpl/scoped_array.h>
25 #include <js_overlay_functions.h>
27 namespace JSCFunctions {
28 std::string ConvertJSStringToStdString(JSStringRef value)
30 int nSize = JSStringGetLength(value) + 1;
31 DPL::ScopedArray<char> textStr(new char[nSize]);
32 JSStringGetUTF8CString(value, textStr.Get(), nSize);
33 std::string ret = textStr.Get();
37 JSValueRef JavaScriptPrintProc(JSContextRef context,
38 JSObjectRef /*object*/,
39 JSObjectRef /*thisObject*/,
41 const JSValueRef arguments[],
42 JSValueRef* exception)
44 if (argumentCount == 0 || !JSValueIsString(context, arguments[0])) {
45 LogError("Argument is not string");
46 return JSValueMakeUndefined(context);
49 JSStringRef textRef = JSValueToStringCopy(context, arguments[0], exception);
50 int nSize = JSStringGetLength(textRef) + 1;
52 DPL::ScopedArray<char> textStr(new char[nSize]);
54 JSStringGetUTF8CString(textRef, textStr.Get(), nSize);
55 LogDebug("\033[00;35m[jsPrint] " << textStr.Get());
57 JSStringRelease(textRef);
58 return JSValueMakeBoolean(context, true);
61 JSValueRef JavaScriptHookProc(
63 JSObjectRef /*object*/,
64 JSObjectRef /*thisObject*/,
66 const JSValueRef arguments[],
67 JSValueRef* exception)
70 if (argumentCount < 2 ||
72 !JSValueIsString(context, arguments[0]) ||
73 !JSValueIsString(context, arguments[1]))
79 LogError("*********************************************");
80 LogError("*********************************************");
81 LogError("Cannot print test Result");
82 LogError("*********************************************");
83 LogError("*********************************************");
84 return JSValueMakeUndefined(context);
87 std::string id, result, message;
88 JSStringRef idRef = JSValueToStringCopy(context, arguments[0], exception);
89 id = ConvertJSStringToStdString(idRef);
90 JSStringRelease(idRef);
91 JSStringRef idResult = JSValueToStringCopy(context,
94 result = ConvertJSStringToStdString(idResult);
95 JSStringRelease(idResult);
97 if (argumentCount == 3 && !JSValueIsString(context, arguments[2])) {
98 JSStringRef idMessage = JSValueToStringCopy(context,
101 message = ConvertJSStringToStdString(idMessage);
102 JSStringRelease(idMessage);
105 LogDebug("\033[00;35m***********************************************");
106 LogDebug("\033[00;35m***********************************************");
107 LogDebug("\033[00;35m TEST ID: " << id);
108 LogDebug("\033[00;35m RESULT: " << result);
109 LogDebug("\033[00;35m MESSAGE: " << message);
110 LogDebug("\033[00;35m***********************************************");
111 LogDebug("\033[00;35m***********************************************");
113 return JSValueMakeBoolean(context, true);