void patchGrammar(xsMachine* the) { xsVars(1); xsVar(0) = xsGet(xsGlobal, xsID_Grammar); xsNewHostProperty(xsVar(0), xsID_nameToID, xsNewHostFunction(Grammar_nameToID, 1), xsDontEnum, xsDontScript); xsNewHostProperty(xsVar(0), xsID_parse, xsNewHostFunction(xs_parse, 1), xsDontEnum, xsDontScript); xsNewHostProperty(xsVar(0), xsID_stringify, xsNewHostFunction(xs_serialize, 1), xsDontEnum, xsDontScript); }
void KPR_Message_patch(xsMachine* the) { xsResult = xsGet(xsGlobal, xsID("Message")); xsNewHostProperty(xsResult, xsID("CHUNK"), xsString("CHUNK"), xsDontDelete | xsDontSet, xsDontScript | xsDontDelete | xsDontSet); xsNewHostProperty(xsResult, xsID("DOM"), xsString("DOM"), xsDontDelete | xsDontSet, xsDontScript | xsDontDelete | xsDontSet); xsNewHostProperty(xsResult, xsID("JSON"), xsString("JSON"), xsDontDelete | xsDontSet, xsDontScript | xsDontDelete | xsDontSet); xsNewHostProperty(xsResult, xsID("TEXT"), xsString("TEXT"), xsDontDelete | xsDontSet, xsDontScript | xsDontDelete | xsDontSet); xsNewHostProperty(xsResult, xsID("URI"), xsNewHostFunction(KPR_Message_URI, 1), xsDefault, xsDontScript); xsNewHostProperty(xsResult, xsID("cancelReferrer"), xsNewHostFunction(KPR_Message_cancelReferrer, 1), xsDefault, xsDontScript); xsNewHostProperty(xsResult, xsID("notify"), xsNewHostFunction(KPR_Message_notify, 1), xsDefault, xsDontScript); }
FskErr KprApplicationNew(KprApplication* it, char* url, char* id, Boolean breakOnStart, Boolean breakOnExceptions) { KprCoordinatesRecord coordinates = { kprLeftRight, kprTopBottom, 0, 0, 0, 0, 0, 0 }; xsAllocation allocation = { 2 * 1024 * 1024, 1024 * 1024, 64 * 1024, 8 * 1024, 2048, 16000, 1993 }; FskErr err = kFskErrNone; KprApplication self; bailIfError(FskMemPtrNewClear(sizeof(KprApplicationRecord), it)); self = *it; FskInstrumentedItemNew(self, NULL, &KprApplicationInstrumentation); self->dispatch = &KprApplicationDispatchRecord; self->flags = kprContainer | kprClip | kprVisible; KprContentInitialize((KprContent)self, &coordinates, NULL, NULL); bailIfError(KprURLMerge(gShell->url, url, &self->url)); if (id) { self->id = FskStrDoCopy(id); bailIfNULL(self->id); } self->the = xsAliasMachine(&allocation, gShell->root, self->url, self); if (!self->the) BAIL(kFskErrMemFull); FskInstrumentedItemSendMessageNormal(self, kprInstrumentedContentCreateMachine, self); xsBeginHost(self->the); xsResult = xsNewHostFunction(KPR_include, 1); xsSet(xsResult, xsID("uri"), xsString(self->url)); xsNewHostProperty(xsGlobal, xsID("include"), xsResult, xsDontDelete | xsDontSet, xsDontScript | xsDontDelete | xsDontSet); xsResult = xsNewHostFunction(KPR_require, 1); xsSet(xsResult, xsID("uri"), xsString(self->url)); xsNewHostProperty(xsGlobal, xsID("require"), xsResult, xsDontDelete | xsDontSet, xsDontScript | xsDontDelete | xsDontSet); xsResult = xsNewInstanceOf(xsGet(xsGet(xsGlobal, xsID("KPR")), xsID("application"))); self->slot = xsResult; xsSetHostData(xsResult, self); (void)xsCall1(xsGet(xsGlobal, xsID("Object")), xsID("seal"), xsResult); xsNewHostProperty(xsGlobal, xsID("application"), xsResult, xsDontDelete | xsDontSet, xsDontScript | xsDontDelete | xsDontSet); xsNewHostProperty(xsGlobal, xsID("shell"), xsNull, xsDontDelete | xsDontSet, xsDontScript | xsDontDelete | xsDontSet); if (breakOnStart) xsDebugger(); if (breakOnExceptions) (void)xsCall1(xsGet(xsGet(xsGlobal, xsID("xs")), xsID("debug")), xsID("setBreakOnException"), xsBoolean(breakOnExceptions)); (void)xsCall1(xsGlobal, xsID("include"), xsString(self->url)); xsEndHost(self->the); KprContentChainPrepend(&gShell->applicationChain, self, 0, NULL); bail: return err; }
FskErr FskSSLHandshake(void *a, FskNetSocketCreatedCallback callback, void *refCon, Boolean initiate) { FskSSL *fssl = a; FskErr err = kFskErrNone; fssl->socketCallback = callback; fssl->callbackData = refCon; xsBeginHost(fssl->vm->the); xsTry { xsVars(1); /* set session._hostData = ssl, just for callback */ xsVar(0) = xsNewHostObject(NULL); xsSetHostData(xsVar(0), fssl); xsSet(fssl->ssl, xsID("_hostData"), xsVar(0)); xsVar(0) = xsNewHostFunction(xs_handshake_finished_callback, 2); xsSet(fssl->ssl, xsID("_hostCallback"), xsVar(0)); xsCall3_noResult(fssl->ssl, xsID("handshake"), fssl->socket, xsVar(0), initiate ? xsTrue : xsFalse); } xsCatch { if (xsHas(xsException, xsID("code"))) err = xsToInteger(xsGet(xsException, xsID("code"))); if (err == kFskErrNone) err = kFskErrOperationFailed; } xsEndHost(fssl->vm->the); return err; }
void KPR_message_invoke(xsMachine* the) { KprMessage self = kprGetHostData(xsThis, this, message); xsVars(1); xsVar(0) = xsNewHostFunction(KPR_Message_invoke_executor, 2); xsSet(xsVar(0), xsID_message, xsThis); if (xsToInteger(xsArgc) > 0) KprMessageScriptTargetSet(self, the, &xsArg(0)); xsResult = xsNew1(xsGlobal, xsID_Promise, xsVar(0)); }
int main(int argc, char* argv[]) { xsCreation creation_mc = { 25*1024, /* initial chunk size */ 2048, /* incremental chunk size */ 50*1024/16, /* initial heap count -- will be calculated later */ 128, /* incremental heap count -- wasting 16 bytes / allocation */ 650, /* stack count */ 2048+1024, /* key count */ 97, /* name modulo */ 127, /* symbol modulo */ }; xsCreation creation_tool = { 128 * 1024 * 1024, /* initialChunkSize */ 16 * 1024 * 1024, /* incrementalChunkSize */ 8 * 1024 * 1024, /* initialHeapCount */ 1 * 1024 * 1024, /* incrementalHeapCount */ 4096, /* stackCount */ 4096*3, /* keyCount */ 1993, /* nameModulo */ 127 /* symbolModulo */ }; xsCreation* creation = &creation_tool; int error = 0; int argi = 1; void* archive = NULL; xsBooleanValue program = 0; xsBooleanValue xsbug = 0; xsMachine* machine; char path[PATH_MAX]; char modulePath[PATH_MAX]; xsStringValue extension; xsStringValue slash; xsStringValue name; int size; if (argi < argc) { if (!strcmp(argv[argi], "-a")) { argi++; if (argi < argc) { if (realpath(argv[argi], path)) { archive = fxMapArchive(path, NULL); if (!archive) { fprintf(stderr, "# invalid archive: %s\n", path); return 1; } if (strstr(path, "mc.xsa")) { creation = &creation_mc; } } else { fprintf(stderr, "# archive not found: %s\n", argv[argi]); return 1; } argi++; } } } while (argi < argc) { if (!strcmp(argv[argi], "-p")) { program = 1; argi++; } else if (!strcmp(argv[argi], "-x")) { xsbug = 1; argi++; } else break; } gargc = argc; gargi = argi; gargv = argv; machine = xsCreateMachine(creation, archive, "xsr6", NULL); xsBeginHost(machine); { xsVars(2); { xsTry { xsVar(0) = xsNewInstanceOf(xsObjectPrototype); if (xsbug) xsVar(1) = xsNewHostFunction(console_log_xsbug, 0); else xsVar(1) = xsNewHostFunction(console_log, 0); xsSet(xsVar(0), xsID("log"), xsVar(1)); xsSet(xsGlobal, xsID("console"), xsVar(0)); xsVar(0) = xsNewHostObject(weakTest); xsVar(1) = xsNewHostConstructor(WeakTest, 1, xsVar(0)); xsSet(xsGlobal, xsID("WeakTest"), xsVar(1)); xsVar(1) = xsNewHostFunction(gc, 0); xsSet(xsGlobal, xsID("gc"), xsVar(1)); xsResult = xsModulePaths(); if (archive) { slash = strrchr(path, mxSeparator); if (slash) { *(slash + 1) = 0; xsCall1(xsResult, xsID("add"), xsString(path)); } } realpath(argv[0], modulePath); slash = strrchr(modulePath, mxSeparator); if (slash) { strcpy(slash + 1, "modules"); size = c_strlen(modulePath); modulePath[size++] = mxSeparator; modulePath[size] = 0; xsCall1(xsResult, xsID("add"), xsString(modulePath)); } if (argi == argc) { fprintf(stderr, "# no module, no program\n"); error = 1; } else if (program) { xsVar(0) = xsNewHostFunction(print, 0); xsSet(xsGlobal, xsID("print"), xsVar(0)); //xsStartProfiling(); while (argi < argc) { if (argv[argi][0] != '-') { xsElseError(realpath(argv[argi], path)); strcpy(modulePath, path); slash = strrchr(modulePath, mxSeparator); *(slash + 1) = 0; xsCall1(xsResult, xsID("add"), xsString(modulePath)); strcat(modulePath, "modules/"); xsCall1(xsResult, xsID("add"), xsString(modulePath)); fxRunProgram(the, path); } argi++; } fxRunLoop(the); //xsStopProfiling(); } else { xsVar(0) = xsNewInstanceOf(xsObjectPrototype); xsVar(1) = xsNewHostFunction(process_cwd, 0); xsSet(xsVar(0), xsID("cwd"), xsVar(1)); #ifdef mxDebug xsSet(xsVar(0), xsID("debug"), xsTrue); #else xsSet(xsVar(0), xsID("debug"), xsFalse); #endif xsVar(1) = xsNewHostFunction(process_execArgv, 0); xsSet(xsVar(0), xsID("execArgv"), xsVar(1)); xsVar(1) = xsNewHostFunction(process_getenv, 0); xsSet(xsVar(0), xsID("getenv"), xsVar(1)); xsVar(1) = xsNewHostFunction(process_then, 1); xsSet(xsVar(0), xsID("then"), xsVar(1)); xsSet(xsVar(0), xsID("platform"), xsString("darwin")); xsSet(xsGlobal, xsID("process"), xsVar(0)); strcpy(path, argv[argi]); slash = strrchr(path, mxSeparator); if (slash) { *slash = 0; realpath(path, modulePath); size = c_strlen(modulePath); modulePath[size++] = mxSeparator; modulePath[size] = 0; xsCall1(xsResult, xsID("add"), xsString(modulePath)); strcat(modulePath, "modules"); size = c_strlen(modulePath); modulePath[size++] = mxSeparator; modulePath[size] = 0; xsCall1(xsResult, xsID("add"), xsString(modulePath)); name = slash + 1; } else { realpath(".", modulePath); size = c_strlen(modulePath); modulePath[size++] = mxSeparator; modulePath[size] = 0; xsCall1(xsResult, xsID("add"), xsString(modulePath)); strcat(modulePath, "modules"); size = c_strlen(modulePath); modulePath[size++] = mxSeparator; modulePath[size] = 0; xsCall1(xsResult, xsID("add"), xsString(modulePath)); name = path; } extension = strrchr(name, '.'); if (extension) *extension = 0; //xsStartProfiling(); fxRunModule(the, name); //xsStopProfiling(); } } xsCatch { xsStringValue message = xsToString(xsException); fprintf(stderr, "### %s\n", message); error = 1; } } } xsEndHost(the); xsDeleteMachine(machine); fxUnmapArchive(archive); if (!error && process_then_parameters) { #if mxWindows error =_spawnvp(_P_WAIT, process_then_parameters[0], process_then_parameters); if (error < 0) fprintf(stderr, "### Cannot execute %s!\n", process_then_parameters[0]); #else execvp(process_then_parameters[0], process_then_parameters); #endif } return error; }
void KPR_shell_execute(xsMachine* the) { xsStringValue application = NULL; xsStringValue command = NULL; xsStringValue directory = NULL; xsStringValue environment = NULL; xsStringValue string; xsIntegerValue length; KprShellExec exec = NULL; STARTUPINFO si; SECURITY_ATTRIBUTES sa; xsVars(5); xsTry { application = getenv("COMSPEC"); if (!application) xsThrowIfFskErr(kFskErrOperationFailed); string = xsToString(xsArg(0)); length = FskStrLen(string) + 1; xsThrowIfFskErr(FskMemPtrNew(3 + length, &command)); memcpy(command, "/c ", 3); memcpy(command + 3, string, length); if (xsFindString(xsArg(1), xsID_directory, &string)) { length = FskStrLen(string) + 1; xsThrowIfFskErr(FskMemPtrNew(length, &directory)); memcpy(directory, string, length); } if (xsFindResult(xsArg(1), xsID_environment)) { xsIntegerValue total = 0, length; xsVar(1) = xsEnumerate(xsResult); for (;;) { xsVar(2) = xsCall0(xsVar(1), xsID("next")); xsVar(3) = xsGet(xsVar(2), xsID("done")); if (xsTest(xsVar(3))) break; xsVar(3) = xsGet(xsVar(2), xsID("value")); xsVar(4) = xsGetAt(xsResult, xsVar(3)); total += FskStrLen(xsToString(xsVar(3))); total++; total += FskStrLen(xsToString(xsVar(4))); total++; } total++; xsThrowIfFskErr(FskMemPtrNew(total, &environment)); total = 0; xsVar(1) = xsEnumerate(xsResult); for (;;) { xsVar(2) = xsCall0(xsVar(1), xsID("next")); xsVar(3) = xsGet(xsVar(2), xsID("done")); if (xsTest(xsVar(3))) break; xsVar(3) = xsGet(xsVar(2), xsID("value")); xsVar(4) = xsGetAt(xsResult, xsVar(3)); string = xsToString(xsVar(3)); length = FskStrLen(string); memcpy(environment + total, string, length); total += length; environment[total++] = '='; string = xsToString(xsVar(4)); length = FskStrLen(string); memcpy(environment + total, string, length); total += length; environment[total++] = 0; } environment[total++] = 0; } xsThrowIfFskErr(FskMemPtrNewClear(sizeof(KprShellExecRecord), &exec)); xsVar(0) = xsNewHostObject(KPR_shell_execute_destructor); exec->the = the; exec->slot = xsVar(0); xsSetHostData(xsVar(0), exec); xsResult = xsNewHostFunction(KPR_shell_execute_cancel, 0); xsNewHostProperty(xsVar(0), xsID_cancel, xsResult, xsDefault, xsDontDelete | xsDontEnum | xsDontSet); if (xsFindResult(xsArg(1), xsID_callback)) { xsNewHostProperty(xsVar(0), xsID_callback, xsResult, xsDefault, xsDontDelete | xsDontEnum | xsDontSet); } else { xsNewHostProperty(xsVar(0), xsID_callback, xsNull, xsDefault, xsDontDelete | xsDontEnum | xsDontSet); } if (xsFindResult(xsArg(1), xsID_stderr)) { xsNewHostProperty(xsVar(0), xsID_stderr, xsResult, xsDefault, xsDontDelete | xsDontEnum | xsDontSet); } else { xsNewHostProperty(xsVar(0), xsID_stderr, xsNull, xsDefault, xsDontDelete | xsDontEnum | xsDontSet); } if (xsFindResult(xsArg(1), xsID_stdout)) { xsNewHostProperty(xsVar(0), xsID_stdout, xsResult, xsDefault, xsDontDelete | xsDontEnum | xsDontSet); } else { xsNewHostProperty(xsVar(0), xsID_stdout, xsNull, xsDefault, xsDontDelete | xsDontEnum | xsDontSet); } sa.nLength = sizeof(SECURITY_ATTRIBUTES); sa.bInheritHandle = TRUE; sa.lpSecurityDescriptor = NULL; if (!CreatePipe(&(exec->hReadPipe), &(exec->hWritePipe), &sa, 0)) xsThrowIfFskErr(kFskErrOperationFailed); if (!SetHandleInformation(exec->hReadPipe, HANDLE_FLAG_INHERIT, 0)) xsThrowIfFskErr(kFskErrOperationFailed); ZeroMemory(&si, sizeof(si)); si.cb = sizeof(STARTUPINFO); si.hStdError = exec->hWritePipe; si.hStdOutput = exec->hWritePipe; si.hStdInput = GetStdHandle(STD_INPUT_HANDLE); si.dwFlags = STARTF_USESTDHANDLES; if (!CreateProcess(application, command, NULL, NULL, TRUE, CREATE_NO_WINDOW, environment, directory, &si, &(exec->pi))) xsThrowIfFskErr(kFskErrOperationFailed); xsRemember(exec->slot); exec->usage++; FskThreadPostCallback(KprHTTPGetThread(), (FskThreadCallback)KPR_shell_execute_async, exec, NULL, NULL, NULL); FskMemPtrDispose(environment); FskMemPtrDispose(directory); FskMemPtrDispose(command); } xsCatch { if (exec) { if (exec->pi.hProcess) CloseHandle(exec->pi.hProcess); if (exec->pi.hThread) CloseHandle(exec->pi.hThread); if (exec->hWritePipe) CloseHandle(exec->hWritePipe); if (exec->hReadPipe) CloseHandle(exec->hReadPipe); FskMemPtrDispose(exec); } FskMemPtrDispose(environment); FskMemPtrDispose(directory); FskMemPtrDispose(command); xsThrow(xsException); } }