static HRESULT WINAPI JScriptParseProcedure_ParseProcedureText(IActiveScriptParseProcedure2 *iface, LPCOLESTR pstrCode, LPCOLESTR pstrFormalParams, LPCOLESTR pstrProcedureName, LPCOLESTR pstrItemName, IUnknown *punkContext, LPCOLESTR pstrDelimiter, CTXARG_T dwSourceContextCookie, ULONG ulStartingLineNumber, DWORD dwFlags, IDispatch **ppdisp) { JScript *This = impl_from_IActiveScriptParseProcedure2(iface); bytecode_t *code; jsdisp_t *dispex; HRESULT hres; TRACE("(%p)->(%s %s %s %s %p %s %s %u %x %p)\n", This, debugstr_w(pstrCode), debugstr_w(pstrFormalParams), debugstr_w(pstrProcedureName), debugstr_w(pstrItemName), punkContext, debugstr_w(pstrDelimiter), wine_dbgstr_longlong(dwSourceContextCookie), ulStartingLineNumber, dwFlags, ppdisp); if(This->thread_id != GetCurrentThreadId() || This->ctx->state == SCRIPTSTATE_CLOSED) return E_UNEXPECTED; hres = compile_script(This->ctx, pstrCode, pstrFormalParams, pstrDelimiter, FALSE, This->is_encode, &code); if(FAILED(hres)) { WARN("Parse failed %08x\n", hres); return hres; } hres = create_source_function(This->ctx, code, &code->global_code, NULL, &dispex); release_bytecode(code); if(FAILED(hres)) return hres; *ppdisp = to_disp(dispex); return S_OK; }
static HRESULT WINAPI JScriptParse_ParseScriptText(IActiveScriptParse *iface, LPCOLESTR pstrCode, LPCOLESTR pstrItemName, IUnknown *punkContext, LPCOLESTR pstrDelimiter, CTXARG_T dwSourceContextCookie, ULONG ulStartingLine, DWORD dwFlags, VARIANT *pvarResult, EXCEPINFO *pexcepinfo) { JScript *This = impl_from_IActiveScriptParse(iface); bytecode_t *code; HRESULT hres; TRACE("(%p)->(%s %s %p %s %s %u %x %p %p)\n", This, debugstr_w(pstrCode), debugstr_w(pstrItemName), punkContext, debugstr_w(pstrDelimiter), wine_dbgstr_longlong(dwSourceContextCookie), ulStartingLine, dwFlags, pvarResult, pexcepinfo); if(This->thread_id != GetCurrentThreadId() || This->ctx->state == SCRIPTSTATE_CLOSED) return E_UNEXPECTED; hres = compile_script(This->ctx, pstrCode, NULL, pstrDelimiter, (dwFlags & SCRIPTTEXT_ISEXPRESSION) != 0, This->is_encode, &code); if(FAILED(hres)) return hres; if(dwFlags & SCRIPTTEXT_ISEXPRESSION) { jsval_t r; IActiveScriptSite_OnEnterScript(This->site); clear_ei(This->ctx); hres = exec_source(This->ctx, EXEC_GLOBAL, code, &code->global_code, NULL, NULL, NULL, This->ctx->global, 0, NULL, &r); if(SUCCEEDED(hres)) { if(pvarResult) hres = jsval_to_variant(r, pvarResult); jsval_release(r); } IActiveScriptSite_OnLeaveScript(This->site); return hres; } /* * Although pvarResult is not really used without SCRIPTTEXT_ISEXPRESSION flag, if it's not NULL, * script is executed immediately, even if it's not in started state yet. */ if(!pvarResult && !is_started(This->ctx)) { if(This->queue_tail) This->queue_tail = This->queue_tail->next = code; else This->queue_head = This->queue_tail = code; return S_OK; } hres = exec_global_code(This, code); release_bytecode(code); if(FAILED(hres)) return hres; if(pvarResult) V_VT(pvarResult) = VT_EMPTY; return S_OK; }
static HRESULT WINAPI JScriptParse_ParseScriptText(IActiveScriptParse *iface, LPCOLESTR pstrCode, LPCOLESTR pstrItemName, IUnknown *punkContext, LPCOLESTR pstrDelimiter, CTXARG_T dwSourceContextCookie, ULONG ulStartingLine, DWORD dwFlags, VARIANT *pvarResult, EXCEPINFO *pexcepinfo) { JScript *This = impl_from_IActiveScriptParse(iface); bytecode_t *code; HRESULT hres; TRACE("(%p)->(%s %s %p %s %s %u %x %p %p)\n", This, debugstr_w(pstrCode), debugstr_w(pstrItemName), punkContext, debugstr_w(pstrDelimiter), wine_dbgstr_longlong(dwSourceContextCookie), ulStartingLine, dwFlags, pvarResult, pexcepinfo); if(This->thread_id != GetCurrentThreadId() || This->ctx->state == SCRIPTSTATE_CLOSED) return E_UNEXPECTED; hres = compile_script(This->ctx, pstrCode, NULL, pstrDelimiter, (dwFlags & SCRIPTTEXT_ISEXPRESSION) != 0, This->is_encode, &code); if(FAILED(hres)) return hres; if(dwFlags & SCRIPTTEXT_ISEXPRESSION) { exec_ctx_t *exec_ctx; hres = create_exec_ctx(This->ctx, NULL, This->ctx->global, NULL, TRUE, &exec_ctx); if(SUCCEEDED(hres)) { jsval_t r; IActiveScriptSite_OnEnterScript(This->site); clear_ei(This->ctx); hres = exec_source(exec_ctx, code, &code->global_code, TRUE, &r); if(SUCCEEDED(hres)) { hres = jsval_to_variant(r, pvarResult); jsval_release(r); } exec_release(exec_ctx); IActiveScriptSite_OnLeaveScript(This->site); } return hres; } if(!is_started(This->ctx)) { if(This->queue_tail) This->queue_tail = This->queue_tail->next = code; else This->queue_head = This->queue_tail = code; return S_OK; } hres = exec_global_code(This, code); release_bytecode(code); return hres; }
void compile_and_run(HSQUIRRELVM vm, std::istream& in, const std::string& sourcename) { compile_script(vm, in, sourcename); SQInteger oldtop = sq_gettop(vm); try { sq_pushroottable(vm); if(SQ_FAILED(sq_call(vm, 1, SQFalse, SQTrue))) throw SquirrelError(vm, "Couldn't start script"); } catch(...) { sq_settop(vm, oldtop); throw; } // we can remove the closure in case the script was not suspended if(sq_getvmstate(vm) != SQ_VMSTATE_SUSPENDED) { sq_settop(vm, oldtop-1); } }
static HRESULT construct_function(script_ctx_t *ctx, unsigned argc, jsval_t *argv, IDispatch **ret) { WCHAR *str = NULL, *ptr; unsigned len = 0, i = 0; bytecode_t *code; jsdisp_t *function; jsstr_t **params = NULL; int j = 0; HRESULT hres = S_OK; static const WCHAR function_anonymousW[] = {'f','u','n','c','t','i','o','n',' ','a','n','o','n','y','m','o','u','s','('}; static const WCHAR function_beginW[] = {')',' ','{','\n'}; static const WCHAR function_endW[] = {'\n','}',0}; if(argc) { params = heap_alloc(argc*sizeof(*params)); if(!params) return E_OUTOFMEMORY; if(argc > 2) len = (argc-2)*2; /* separating commas */ for(i=0; i < argc; i++) { hres = to_string(ctx, argv[i], params+i); if(FAILED(hres)) break; len += jsstr_length(params[i]); } } if(SUCCEEDED(hres)) { len += (sizeof(function_anonymousW) + sizeof(function_beginW) + sizeof(function_endW)) / sizeof(WCHAR); str = heap_alloc(len*sizeof(WCHAR)); if(str) { memcpy(str, function_anonymousW, sizeof(function_anonymousW)); ptr = str + sizeof(function_anonymousW)/sizeof(WCHAR); if(argc > 1) { while(1) { ptr += jsstr_flush(params[j], ptr); if(++j == argc-1) break; *ptr++ = ','; *ptr++ = ' '; } } memcpy(ptr, function_beginW, sizeof(function_beginW)); ptr += sizeof(function_beginW)/sizeof(WCHAR); if(argc) ptr += jsstr_flush(params[argc-1], ptr); memcpy(ptr, function_endW, sizeof(function_endW)); TRACE("%s\n", debugstr_w(str)); }else { hres = E_OUTOFMEMORY; } } while(i) jsstr_release(params[--i]); heap_free(params); if(FAILED(hres)) return hres; hres = compile_script(ctx, str, NULL, NULL, FALSE, FALSE, &code); heap_free(str); if(FAILED(hres)) return hres; if(code->global_code.func_cnt != 1 || code->global_code.var_cnt) { ERR("Invalid parser result!\n"); release_bytecode(code); return E_UNEXPECTED; } hres = create_source_function(ctx, code, code->global_code.funcs, NULL, &function); release_bytecode(code); if(FAILED(hres)) return hres; *ret = to_disp(function); return S_OK; }
static int adsi_prog(struct ast_channel *chan, const char *script) { struct adsi_script *scr; int x, bytes; unsigned char buf[1024]; if (!(scr = compile_script(script))) return -1; /* Start an empty ADSI Session */ if (ast_adsi_load_session(chan, NULL, 0, 1) < 1) return -1; /* Now begin the download attempt */ if (ast_adsi_begin_download(chan, scr->desc, scr->fdn, scr->sec, scr->ver)) { /* User rejected us for some reason */ ast_verb(3, "User rejected download attempt\n"); ast_log(LOG_NOTICE, "User rejected download on channel %s\n", chan->name); ast_free(scr); return -1; } bytes = 0; /* Start with key definitions */ for (x = 0; x < scr->numkeys; x++) { if (bytes + scr->keys[x].retstrlen > 253) { /* Send what we've collected so far */ if (ast_adsi_transmit_message(chan, buf, bytes, ADSI_MSG_DOWNLOAD)) { ast_log(LOG_WARNING, "Unable to send chunk ending at %d\n", x); return -1; } bytes =0; } memcpy(buf + bytes, scr->keys[x].retstr, scr->keys[x].retstrlen); bytes += scr->keys[x].retstrlen; #ifdef DUMP_MESSAGES dump_message("Key", scr->keys[x].vname, scr->keys[x].retstr, scr->keys[x].retstrlen); #endif } if (bytes) { if (ast_adsi_transmit_message(chan, buf, bytes, ADSI_MSG_DOWNLOAD)) { ast_log(LOG_WARNING, "Unable to send chunk ending at %d\n", x); return -1; } } bytes = 0; /* Continue with the display messages */ for (x = 0; x < scr->numdisplays; x++) { if (bytes + scr->displays[x].datalen > 253) { /* Send what we've collected so far */ if (ast_adsi_transmit_message(chan, buf, bytes, ADSI_MSG_DOWNLOAD)) { ast_log(LOG_WARNING, "Unable to send chunk ending at %d\n", x); return -1; } bytes =0; } memcpy(buf + bytes, scr->displays[x].data, scr->displays[x].datalen); bytes += scr->displays[x].datalen; #ifdef DUMP_MESSAGES dump_message("Display", scr->displays[x].vname, scr->displays[x].data, scr->displays[x].datalen); #endif } if (bytes) { if (ast_adsi_transmit_message(chan, buf, bytes, ADSI_MSG_DOWNLOAD)) { ast_log(LOG_WARNING, "Unable to send chunk ending at %d\n", x); return -1; } } bytes = 0; /* Send subroutines */ for (x = 0; x < scr->numsubs; x++) { if (bytes + scr->subs[x].datalen > 253) { /* Send what we've collected so far */ if (ast_adsi_transmit_message(chan, buf, bytes, ADSI_MSG_DOWNLOAD)) { ast_log(LOG_WARNING, "Unable to send chunk ending at %d\n", x); return -1; } bytes =0; } memcpy(buf + bytes, scr->subs[x].data, scr->subs[x].datalen); bytes += scr->subs[x].datalen; #ifdef DUMP_MESSAGES dump_message("Sub", scr->subs[x].vname, scr->subs[x].data, scr->subs[x].datalen); #endif } if (bytes) { if (ast_adsi_transmit_message(chan, buf, bytes, ADSI_MSG_DOWNLOAD)) { ast_log(LOG_WARNING, "Unable to send chunk ending at %d\n", x); return -1; } } bytes = 0; bytes += ast_adsi_display(buf, ADSI_INFO_PAGE, 1, ADSI_JUST_LEFT, 0, "Download complete.", ""); bytes += ast_adsi_set_line(buf, ADSI_INFO_PAGE, 1); if (ast_adsi_transmit_message(chan, buf, bytes, ADSI_MSG_DISPLAY) < 0) return -1; if (ast_adsi_end_download(chan)) { /* Download failed for some reason */ ast_verb(3, "Download attempt failed\n"); ast_log(LOG_NOTICE, "Download failed on %s\n", chan->name); ast_free(scr); return -1; } ast_free(scr); ast_adsi_unload_session(chan); return 0; }
static HRESULT construct_function(script_ctx_t *ctx, DISPPARAMS *dp, jsexcept_t *ei, IDispatch **ret) { WCHAR *str = NULL, *ptr; DWORD argc, len = 0, l; bytecode_t *code; jsdisp_t *function; BSTR *params = NULL; int i=0, j=0; HRESULT hres = S_OK; static const WCHAR function_anonymousW[] = {'f','u','n','c','t','i','o','n',' ','a','n','o','n','y','m','o','u','s','('}; static const WCHAR function_beginW[] = {')',' ','{','\n'}; static const WCHAR function_endW[] = {'\n','}',0}; argc = arg_cnt(dp); if(argc) { params = heap_alloc(argc*sizeof(BSTR)); if(!params) return E_OUTOFMEMORY; if(argc > 2) len = (argc-2)*2; /* separating commas */ for(i=0; i < argc; i++) { hres = to_string(ctx, get_arg(dp,i), ei, params+i); if(FAILED(hres)) break; len += SysStringLen(params[i]); } } if(SUCCEEDED(hres)) { len += (sizeof(function_anonymousW) + sizeof(function_beginW) + sizeof(function_endW)) / sizeof(WCHAR); str = heap_alloc(len*sizeof(WCHAR)); if(str) { memcpy(str, function_anonymousW, sizeof(function_anonymousW)); ptr = str + sizeof(function_anonymousW)/sizeof(WCHAR); if(argc > 1) { while(1) { l = SysStringLen(params[j]); memcpy(ptr, params[j], l*sizeof(WCHAR)); ptr += l; if(++j == argc-1) break; *ptr++ = ','; *ptr++ = ' '; } } memcpy(ptr, function_beginW, sizeof(function_beginW)); ptr += sizeof(function_beginW)/sizeof(WCHAR); if(argc) { l = SysStringLen(params[argc-1]); memcpy(ptr, params[argc-1], l*sizeof(WCHAR)); ptr += l; } memcpy(ptr, function_endW, sizeof(function_endW)); TRACE("%s\n", debugstr_w(str)); }else { hres = E_OUTOFMEMORY; } } while(--i >= 0) SysFreeString(params[i]); heap_free(params); if(FAILED(hres)) return hres; hres = compile_script(ctx, str, NULL, FALSE, FALSE, &code); heap_free(str); if(FAILED(hres)) return hres; if(code->global_code.func_cnt != 1 || code->global_code.var_cnt) { ERR("Invalid parser result!\n"); release_bytecode(code); return E_UNEXPECTED; } hres = create_source_function(ctx, code, code->global_code.funcs, NULL, &function); release_bytecode(code); if(FAILED(hres)) return hres; *ret = to_disp(function); return S_OK; }