예제 #1
0
HRESULT ScriptHost::Initialize()
{
	Finalize();

	m_has_error = false;

	HRESULT hr = S_OK;
	IActiveScriptParsePtr parser;
	pfc::stringcvt::string_wide_from_utf8_fast wname(m_host->get_script_engine());
	pfc::stringcvt::string_wide_from_utf8_fast wcode(m_host->get_script_code());
	// Load preprocessor module
	script_preprocessor preprocessor(wcode.get_ptr());
	preprocessor.process_script_info(m_host->ScriptInfo());

    hr = InitScriptEngineByName(wname);

	if (SUCCEEDED(hr)) hr = m_script_engine->SetScriptSite(this);
	if (SUCCEEDED(hr)) hr = m_script_engine->QueryInterface(&parser);
	if (SUCCEEDED(hr)) hr = parser->InitNew();

    EnableSafeModeToScriptEngine(m_script_engine, g_cfg_safe_mode);

	if (SUCCEEDED(hr)) hr = m_script_engine->AddNamedItem(L"window", SCRIPTITEM_ISVISIBLE);
	if (SUCCEEDED(hr)) hr = m_script_engine->AddNamedItem(L"gdi", SCRIPTITEM_ISVISIBLE);
	if (SUCCEEDED(hr)) hr = m_script_engine->AddNamedItem(L"fb", SCRIPTITEM_ISVISIBLE);
	if (SUCCEEDED(hr)) hr = m_script_engine->AddNamedItem(L"utils", SCRIPTITEM_ISVISIBLE);
    if (SUCCEEDED(hr)) hr = m_script_engine->AddNamedItem(L"plman", SCRIPTITEM_ISVISIBLE);
	if (SUCCEEDED(hr)) hr = m_script_engine->AddNamedItem(L"et", SCRIPTITEM_ISVISIBLE);
	if (SUCCEEDED(hr)) hr = m_script_engine->SetScriptState(SCRIPTSTATE_CONNECTED);
	if (SUCCEEDED(hr)) hr = m_script_engine->GetScriptDispatch(NULL, &m_script_root);
    // Parse imported scripts
    if (SUCCEEDED(hr)) hr = ProcessImportedScripts(preprocessor, parser);

	// Parse main script
	DWORD source_context = 0;
	if (SUCCEEDED(hr)) hr = GenerateSourceContext(NULL, wcode, source_context);
    m_contextToPathMap[source_context] = "<main>";

	if (SUCCEEDED(hr)) hr = parser->ParseScriptText(wcode.get_ptr(), NULL, NULL, NULL, 
		source_context, 0, SCRIPTTEXT_HOSTMANAGESSOURCE | SCRIPTTEXT_ISVISIBLE, NULL, NULL);

	if (SUCCEEDED(hr))
    {
        m_engine_inited = true;
    }
    else
    {
        m_engine_inited = false;
        m_has_error = true;
    }

    m_callback_invoker.init(m_script_root);
	return hr;
}
예제 #2
0
HRESULT ScriptHost::ProcessImportedScripts(script_preprocessor &preprocessor, IActiveScriptParsePtr &parser)
{
    // processing "@import"
    script_preprocessor::t_script_list scripts;
    HRESULT hr = preprocessor.process_import(m_host->ScriptInfo(), scripts);

    for (t_size i = 0; i < scripts.get_count(); ++i)
    {
        DWORD source_context;

        if (SUCCEEDED(hr)) hr = GenerateSourceContext(scripts[i].path.get_ptr(), scripts[i].code.get_ptr(), source_context);
        if (FAILED(hr)) break;

        m_contextToPathMap[source_context] = pfc::stringcvt::string_utf8_from_wide(scripts[i].path.get_ptr());
        hr = parser->ParseScriptText(scripts[i].code.get_ptr(), NULL, NULL, NULL, 
            source_context, 0, SCRIPTTEXT_HOSTMANAGESSOURCE | SCRIPTTEXT_ISVISIBLE, NULL, NULL);
    }

    return hr;
}
예제 #3
0
/**
 * Evaluate JavaScript expression.
 *
 * @parem [in] lpwszScript JavaScript expression
 * @parem [out] lpwszResult evaluation result
 * @return S_OK if successful.
 */
HRESULT JScriptEngine::Eval(LPCWSTR lpwszScript, LPWSTR lpwszResult)
{
    HRESULT hr = E_FAIL;

    if (m_spScript == NULL)
      return E_FAIL;

    IActiveScriptPtr spScript;

    // Clone the scripting engine.
    hr = m_spScript->Clone(&spScript);
    if (FAILED(hr))
    {
#ifdef _DEBUG
	OutputDebugString(TEXT("Unable to clone JScript engine."));
#endif
	return hr;	
    }

    
    IActiveScriptParsePtr spScriptParse;
    
    // Script Engine must support IActiveScriptParse for us to use it
    hr = spScript->QueryInterface(IID_IActiveScriptParse, (void **)&spScriptParse);
    if (FAILED(hr))
    {
#ifdef _DEBUG
	OutputDebugString(TEXT("JScript engine doesn't support IActiveScriptParse."));
#endif
	return hr;
    }
  
    IActiveScriptSitePtr spScriptSite;

    CComObject<JScriptSite>* pJScriptSite = NULL;
    CComObject<JScriptSite>::CreateInstance(&pJScriptSite);

    pJScriptSite->QueryInterface(IID_IActiveScriptSite, (void**)&spScriptSite); 

    // Set scripting site
    hr = spScript->SetScriptSite(spScriptSite);
    if (hr)
	return hr;

    // InitNew the object:
    hr = spScriptParse->InitNew();

    VARIANT v;
    VariantInit(&v);

    // Feed the custom JavaScript to the JavaScript engine.
    hr = spScriptParse->ParseScriptText(lpwszScript, NULL, NULL, NULL, 0, 0, SCRIPTTEXT_ISEXPRESSION , &v, NULL);

    if (v.vt == VT_BSTR)
    {
	wsprintfW(lpwszResult, L"%s", v.bstrVal);
    }
    
    VariantClear(&v);

    return hr;
}