1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292
|
/*
* Copyright (C) 2007 Apple Inc. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* 3. Neither the name of Apple Computer, Inc. ("Apple") nor the names of
* its contributors may be used to endorse or promote products derived
* from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY APPLE AND ITS CONTRIBUTORS "AS IS" AND ANY
* EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL APPLE OR ITS CONTRIBUTORS BE LIABLE FOR ANY
* DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
* THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#include "config.h"
#include "WebScriptCallFrame.h"
#include "COMEnumVariant.h"
#include "Function.h"
#include "WebKitDLL.h"
#include <JavaScriptCore/Interpreter.h>
#include <JavaScriptCore/JSGlobalObject.h>
#include <JavaScriptCore/JSStringRefBSTR.h>
#include <JavaScriptCore/JSValueRef.h>
#include <JavaScriptCore/PropertyNameArray.h>
#pragma warning(push, 0)
#include <WebCore/BString.h>
#include <WebCore/PlatformString.h>
#pragma warning(pop)
#include <wtf/Assertions.h>
using namespace KJS;
UString WebScriptCallFrame::jsValueToString(KJS::ExecState* state, JSValue* jsvalue)
{
if (!jsvalue)
return "undefined";
switch (jsvalue->type()) {
case NullType:
case UndefinedType:
case UnspecifiedType:
case GetterSetterType:
break;
case StringType:
return jsvalue->getString();
break;
case NumberType:
return UString::from(jsvalue->getNumber());
break;
case BooleanType:
return jsvalue->getBoolean() ? "True" : "False";
break;
case ObjectType:
jsvalue = jsvalue->getObject()->defaultValue(state, StringType);
return jsvalue->getString();
break;
}
return "undefined";
}
// WebScriptCallFrame -----------------------------------------------------------
static ExecState* callingFunctionOrGlobalExecState(ExecState* exec)
{
#if 0
for (ExecState* current = exec; current; current = current->callingExecState())
if (current->codeType() == FunctionCode || current->codeType() == GlobalCode)
return current;
#endif
return 0;
}
WebScriptCallFrame::WebScriptCallFrame(ExecState* state)
: m_refCount(0)
, m_state(callingFunctionOrGlobalExecState(state))
{
ASSERT_ARG(state, state);
ASSERT(m_state);
gClassCount++;
}
WebScriptCallFrame::~WebScriptCallFrame()
{
gClassCount--;
}
WebScriptCallFrame* WebScriptCallFrame::createInstance(ExecState* state)
{
WebScriptCallFrame* instance = new WebScriptCallFrame(state);
instance->AddRef();
return instance;
}
// IUnknown ------------------------------------------------------------------
HRESULT STDMETHODCALLTYPE WebScriptCallFrame::QueryInterface(REFIID riid, void** ppvObject)
{
*ppvObject = 0;
if (IsEqualGUID(riid, IID_IUnknown))
*ppvObject = static_cast<IWebScriptCallFrame*>(this);
else if (IsEqualGUID(riid, IID_IWebScriptCallFrame))
*ppvObject = static_cast<IWebScriptCallFrame*>(this);
else
return E_NOINTERFACE;
AddRef();
return S_OK;
}
ULONG STDMETHODCALLTYPE WebScriptCallFrame::AddRef()
{
return ++m_refCount;
}
ULONG STDMETHODCALLTYPE WebScriptCallFrame::Release()
{
ULONG newRef = --m_refCount;
if (!newRef)
delete(this);
return newRef;
}
// IWebScriptCallFrame -----------------------------------------------------------
HRESULT STDMETHODCALLTYPE WebScriptCallFrame::caller(
/* [out, retval] */ IWebScriptCallFrame** callFrame)
{
if (!callFrame)
return E_POINTER;
#if 0
*callFrame = m_state->callingExecState() ? WebScriptCallFrame::createInstance(m_state->callingExecState()) : 0;
#endif
return S_OK;
}
HRESULT STDMETHODCALLTYPE WebScriptCallFrame::functionName(
/* [out, retval] */ BSTR* funcName)
{
if (!funcName)
return E_POINTER;
*funcName = 0;
#if 0
if (!m_state->scopeNode())
return S_OK;
FunctionImp* func = m_state->function();
if (!func)
return E_FAIL;
const Identifier& funcIdent = func->functionName();
if (!funcIdent.isEmpty())
*funcName = WebCore::BString(funcIdent).release();
#endif
return S_OK;
}
HRESULT STDMETHODCALLTYPE WebScriptCallFrame::stringByEvaluatingJavaScriptFromString(
/* [in] */ BSTR script,
/* [out, retval] */ BSTR* result)
{
if (!script)
return E_FAIL;
if (!result)
return E_POINTER;
*result = 0;
JSLock lock;
JSValue* scriptExecutionResult = valueByEvaluatingJavaScriptFromString(script);
*result = WebCore::BString(jsValueToString(m_state, scriptExecutionResult)).release();
return S_OK;
}
HRESULT STDMETHODCALLTYPE WebScriptCallFrame::variableNames(
/* [out, retval] */ IEnumVARIANT** variableNames)
{
if (!variableNames)
return E_POINTER;
*variableNames = 0;
PropertyNameArray propertyNames;
#if 0
m_state->scopeChain().top()->getPropertyNames(m_state, propertyNames);
// FIXME: It would be more efficient to use ::adopt here, but PropertyNameArray doesn't have a swap function.
*variableNames = COMEnumVariant<PropertyNameArray>::createInstance(propertyNames);
#endif
return S_OK;
}
HRESULT STDMETHODCALLTYPE WebScriptCallFrame::valueForVariable(
/* [in] */ BSTR key,
/* [out, retval] */ BSTR* value)
{
if (!key)
return E_FAIL;
if (!value)
return E_POINTER;
*value = 0;
Identifier identKey(reinterpret_cast<UChar*>(key), SysStringLen(key));
JSValue* jsvalue = 0;
#if 0
ScopeChain scopeChain = m_state->scopeChain();
for (ScopeChainIterator it = scopeChain.begin(); it != scopeChain.end() && !jsvalue; ++it)
jsvalue = (*it)->get(m_state, identKey);
*value = WebCore::BString(jsValueToString(m_state, jsvalue)).release();
#endif
return S_OK;
}
JSValue* WebScriptCallFrame::valueByEvaluatingJavaScriptFromString(BSTR script)
{
#if 0
ExecState* state = m_state;
JSGlobalObject* globObj = state->dynamicGlobalObject();
// find "eval"
JSObject* eval = 0;
if (state->scopeNode()) { // "eval" won't work without context (i.e. at global scope)
JSValue* v = globObj->get(state, "eval");
if (v->isObject() && static_cast<JSObject*>(v)->implementsCall())
eval = static_cast<JSObject*>(v);
else
// no "eval" - fallback operates on global exec state
state = globObj->globalExec();
}
JSValue* savedException = state->exception();
state->clearException();
UString code(reinterpret_cast<UChar*>(script), SysStringLen(script));
// evaluate
JSValue* scriptExecutionResult;
if (eval) {
List args;
args.append(jsString(code));
scriptExecutionResult = eval->call(state, 0, args);
} else
// no "eval", or no context (i.e. global scope) - use global fallback
scriptExecutionResult = Interpreter::evaluate(state, UString(), 0, code.data(), code.size(), globObj).value();
if (state->hadException())
scriptExecutionResult = state->exception(); // (may be redundant depending on which eval path was used)
state->setException(savedException);
return scriptExecutionResult;
#else
return jsNull();
#endif
}
template<> struct COMVariantSetter<Identifier>
{
static void setVariant(VARIANT* variant, const Identifier& value)
{
ASSERT(V_VT(variant) == VT_EMPTY);
V_VT(variant) = VT_BSTR;
V_BSTR(variant) = WebCore::BString(reinterpret_cast<const wchar_t*>(value.data()), value.size()).release();
}
};
|