void KPR_host_set_profiling(xsMachine* the) { KprHost self = xsGetHostData(xsThis); KprApplication application = (KprApplication)self->first; Boolean flag = xsToBoolean(xsArg(0)); xsBeginHost(application->the); { if (flag) (void)xsCall0(xsGet(xsGlobal, xsID("xs")), xsID("startProfiling")); else (void)xsCall0(xsGet(xsGlobal, xsID("xs")), xsID("stopProfiling")); } xsEndHost(application->the); }
void KprPinsInvoke(KprService service, KprMessage message) { FskErr err = kFskErrNone; KprPins self = gPins; if (KprMessageContinue(message)) { KprPinsListener listener = NULL; KprPinsListenerFind(&listener, self, message); if (listener) listener->useCount++; if (!FskStrCompareWithLength(message->parts.path, "close", message->parts.pathLength)) { if (listener) { xsBeginHostSandboxCode(listener->the, NULL); { { xsTry { (void)xsCall0(xsAccess(listener->pins), xsID("close")); } xsCatch { err = exceptionToFskErr(the); } } while (listener->pollers) KprPinsPollerDispose(listener->pollers); } xsEndHostSandboxCode(); KprPinsListenerDispose(listener, self); listener = NULL; } else { err = kFskErrNotFound; } } else if (!FskStrCompareWithLength(message->parts.path, "configure", message->parts.pathLength)) {
void KprDebugMachineDataReader(FskThreadDataHandler handler UNUSED, FskThreadDataSource source UNUSED, void *refCon) { FskErr err = kFskErrNone; KprDebugMachine self = refCon; KprDebug debug = self->debug; int size; err = FskNetSocketRecvTCP(self->socket, self->buffer, XS_BUFFER_COUNT, &size); if (size > 0) { xsBeginHostSandboxCode(debug->the, debug->code); xsVars(4); KprDebugMachineParse(self, self->buffer, size); if (self->done) { self->state = XS_HEADER_STATE; self->dataIndex = 0; if (self->broken) { xsCall0(xsGet(xsGlobal, xsID_KPR), xsID_gotoFront); } else { xsCall1(debug->slot, xsID_go, xsString(self->address)); KprDebugMachineCallback(self, "gone"); } } xsEndHostSandboxCode(); } if (err) { KprDebugMachineDispose(self); } }
void KPR_host_clearAllBreakpoints(xsMachine* the) { #ifdef mxDebug KprHost self = xsGetHostData(xsThis); KprApplication application = (KprApplication)self->first; xsBeginHost(application->the); { (void)xsCall0(xsGet(xsGet(xsGlobal, xsID("xs")), xsID("debug")), xsID("clearAllBreakpoints")); } xsEndHost(application->the); #endif }
void KPR_host_get_profiling(xsMachine* the) { KprHost self = xsGetHostData(xsThis); KprApplication application = (KprApplication)self->first; Boolean result = false; xsBeginHost(application->the); { result = xsTest(xsCall0(xsGet(xsGlobal, xsID("xs")), xsID("isProfiling"))); } xsEndHost(application->the); xsResult = xsBoolean(result); }
void KPR_host_get_debugging(xsMachine* the) { #ifdef mxDebug KprHost self = xsGetHostData(xsThis); KprApplication application = (KprApplication)self->first; Boolean result = false; xsBeginHost(application->the); { result = xsTest(xsCall0(xsGet(xsGet(xsGlobal, xsID("xs")), xsID("debug")), xsID("getConnected"))); } xsEndHost(application->the); xsResult = xsBoolean(result); #endif }
void KPR_host_get_profilingDirectory(xsMachine* the) { KprHost self = xsGetHostData(xsThis); KprApplication application = (KprApplication)self->first; FskErr err = kFskErrNone; char* directory = NULL; xsBeginHost(application->the); { xsResult = xsCall0(xsGet(xsGlobal, xsID("xs")), xsID("getProfilingDirectory")); if (xsTest(xsResult)) err = KprPathToURL(xsToString(xsResult), &directory); } xsEndHost(application->the); if (directory) { xsResult = xsString(directory); FskMemPtrDispose(directory); } else { xsThrowIfFskErr(err); } }
void console_log(xsMachine* the) { xsIntegerValue c = xsToInteger(xsArgc), i; xsBooleanValue space = 0; xsVars(4); console_log_depth++; for (i = 0; i < c; i++) { if (space) fprintf(stdout, " "); else space = 1; switch (xsTypeOf(xsArg(i))) { case xsUndefinedType: case xsNullType: case xsBooleanType: case xsIntegerType: case xsNumberType: fprintf(stdout, "%s", xsToString(xsArg(i))); break; case xsSymbolType: xsResult = xsCall1(xsGlobal, xsID("String"), xsArg(i)); fprintf(stdout, "%s", xsToString(xsResult)); break; case xsStringType: case xsStringXType: if ((console_log_depth == 1) && (i == 0)) fprintf(stdout, "%s", xsToString(xsArg(i))); else fprintf(stdout, "'%s'", xsToString(xsArg(i))); break; case xsReferenceType: if (console_log_depth < 3) { xsBooleanValue comma = 0; if (xsHas(xsArg(i), xsID("length"))) { xsIntegerValue length = xsToInteger(xsGet(xsArg(i), xsID("length"))), index; fprintf(stdout, "["); for (index = 0; index < length; index++) { if (comma) fprintf(stdout, ","); else comma = 1; fprintf(stdout, " "); if (xsHas(xsArg(i), (xsIndex)index)) { xsVar(1) = xsGet(xsArg(i), (xsIndex)index); fxPush(xsVar(1)); fxPushCount(the, 1); fxPush(xsThis); fxPush(xsFunction); fxCall(the); } } fprintf(stdout, " ]"); } else { fprintf(stdout, "{"); xsVar(0) = xsEnumerate(xsArg(i)); for (;;) { xsVar(1) = xsCall0(xsVar(0), xsID("next")); if (xsTest(xsGet(xsVar(1), xsID("done")))) break; xsVar(2) = xsGet(xsVar(1), xsID("value")); xsVar(3) = xsGetAt(xsArg(i), xsVar(2)); if (comma) fprintf(stdout, ","); else comma = 1; fprintf(stdout, " %s: ", xsToString(xsVar(2))); fxPush(xsVar(3)); fxPushCount(the, 1); fxPush(xsThis); fxPush(xsFunction); fxCall(the); } fprintf(stdout, " }"); } } else fprintf(stdout, "%s", xsToString(xsArg(i))); break; } } console_log_depth--; if (!console_log_depth) fprintf(stdout, "\n"); }
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); } }
void xs_console_log(xsMachine* the) { xsIntegerValue c = xsToInteger(xsArgc), i; xsBooleanValue space = 0; xsBooleanValue comma = 0; xsBooleanValue nonl = 0; int options = 0; FILE *console_out = stdout; xsVars(4); console_log_depth++; for (i = 0; i < c; i++) { if (space && !nonl) CONSOLE_OUT("\n"); else space = 1; switch (xsTypeOf(xsArg(i))) { case xsUndefinedType: case xsNullType: case xsBooleanType: case xsIntegerType: case xsNumberType: CONSOLE_OUT("%s", xsToString(xsArg(i))); break; case xsSymbolType: xsResult = xsCall1(xsGlobal, xsID("String"), xsArg(i)); CONSOLE_OUT("%s", xsToString(xsResult)); break; case xsStringType: case xsStringXType: if (i == 0 || options) { const char *opt = xsToString(xsArg(i)); options = 0; if (strcmp(opt, "-n") == 0) { nonl++; options++; } else if (strcmp(opt, "-stderr") == 0) { console_out = stderr; options++; } else if (strcmp(opt, "-stdout") == 0) { console_out = stdout; options++; } else if (strncmp(opt, "-", 1) == 0) /* end of the option args */ break; if (options) { space = 0; break; } } if (console_log_depth == 1) CONSOLE_OUT("%s", xsToString(xsArg(i))); else CONSOLE_OUT("\"%s\"", xsToString(xsArg(i))); break; case xsReferenceType: if (xsIsInstanceOf(xsArg(i), xsFunctionPrototype)) { CONSOLE_OUT("function(...) { ... }"); } else if (xsIsInstanceOf(xsArg(i), xsArrayPrototype)) { xsGet(xsVar(0), xsArg(i), xsID("length")); xsIntegerValue length = xsToInteger(xsVar(0)), index; CONSOLE_OUT("[\n"); for (index = 0; index < length; index++) { xsGet(xsVar(1), xsArg(i), (xsIndex)index); if (comma) CONSOLE_OUT(",\n"); else comma = 1; indent(the, console_out, 0); fxPush(xsVar(1)); fxPushCount(the, 1); fxPush(xsThis); fxPush(xsFunction); fxCall(the); } CONSOLE_OUT("\n"); indent(the, console_out, -1); CONSOLE_OUT("]"); } else { CONSOLE_OUT( "{\n"); xsVar(0) = xsEnumerate(xsArg(i)); for (;;) { xsVar(1) = xsCall0(xsVar(0), xsID("next")); xsGet(xsVar(2), xsVar(1), xsID("done")); if (xsTest(xsVar(2))) break; xsGet(xsVar(2), xsVar(1), xsID("value")); xsGetAt(xsVar(3), xsArg(i), xsVar(2)); if (comma) CONSOLE_OUT( ",\n"); else comma = 1; indent(the, console_out, 0); CONSOLE_OUT("%s: ", xsToString(xsVar(2))); fxPush(xsVar(3)); fxPushCount(the, 1); fxPush(xsThis); fxPush(xsFunction); fxCall(the); } CONSOLE_OUT("\n"); indent(the, console_out, -1); CONSOLE_OUT("}"); } break; } } console_log_depth--; if (!console_log_depth && !nonl) CONSOLE_OUT("\n"); }