void KprMessageScriptTargetTransform(void* it, KprMessage message, xsMachine* machine) { KprMessageScriptTarget self = it; if (message->error) return; xsBeginHostSandboxCode(machine, NULL); { xsVars(2); { xsTry { void* data; UInt32 size; KprMessageGetResponseBody(message, &data, &size); if (data && size) { xsIndex id = xsID(self->name); if (xsHas(xsGlobal, id)) { xsVar(0) = xsGet(xsGlobal, id); xsVar(1) = xsNewInstanceOf(xsChunkPrototype); xsSetHostData(xsVar(1), data); xsSetHostDestructor(xsVar(1) , NULL); xsSet(xsVar(1), xsID("length"), xsInteger(size)); xsResult = xsCall1(xsVar(0), xsID("parse"), xsVar(1)); self->result = xsMarshall(xsResult); } } } xsCatch { } } } xsEndHostSandboxCode(); }
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 xs_gpio_init(xsMachine* the) { FskErr err; FskGPIO gpio; SInt32 pin = 0; GPIOdirection dir; char *pinName = NULL; xsVars(1); if ((gpio = xsGetHostData(xsThis))) xsThrowDiagnosticIfFskErr(kFskErrOperationFailed, "Digital pin %d already initialized.", (int)gpio->pinNum); dir = stringToDirection(the, xsToString(xsGet(xsThis, xsID("_direction"))), gpio); xsVar(0) = xsGet(xsThis, xsID("_pin")); if (xsStringType == xsTypeOf(xsVar(0))) pinName = xsToString(xsVar(0)); else pin = xsToInteger(xsVar(0)); err = FskGPIONew(&gpio, pin, pinName, dir); xsThrowDiagnosticIfFskErr(err, "Digital pin %d initialization failed with error %s.", pin, FskInstrumentationGetErrorString(err)); xsSetHostData(xsThis, gpio); }
FskErr FskSSLWrite(void *a, const void *buf, int *bufLen) { FskSSL *fssl = a; FskErr err = kFskErrNone; int len = *bufLen; if (fssl->skt == NULL) return kFskErrOperationFailed; if (buf == NULL || len == 0) return kFskErrNone; xsBeginHost(fssl->vm->the); xsTry { xsVars(1); xsCall1_noResult(fssl->socket, xsID("attachData"), xsHostData(fssl->skt)); xsVar(0) = xsNew1(xsGlobal, xsID("Chunk"), xsInteger(len)); FskMemCopy(xsGetHostData(xsVar(0)), buf, len); xsResult = xsCall2(fssl->ssl, xsID("write"), fssl->socket, xsVar(0)); *bufLen = xsToInteger(xsResult); if (*bufLen == 0) err = kFskErrNoData; xsCall0_noResult(xsVar(0), xsID("free")); } 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_canvasRadialGradient_setStyle(xsMachine *the) { FskCanvas2dContext ctx = xsGetHostData(xsArg(0)); xsNumberValue x0 = xsToNumber(xsGet(xsThis, xsID("x0"))); xsNumberValue y0 = xsToNumber(xsGet(xsThis, xsID("y0"))); xsNumberValue r0 = xsToNumber(xsGet(xsThis, xsID("r0"))); xsNumberValue x1 = xsToNumber(xsGet(xsThis, xsID("x1"))); xsNumberValue y1 = xsToNumber(xsGet(xsThis, xsID("y1"))); xsNumberValue r1 = xsToNumber(xsGet(xsThis, xsID("r1"))); UInt32 c, i; FskCanvas2dGradientStop stops[kCanvas2DMaxGradientStops]; xsVars(2); xsVar(0) = xsGet(xsThis, xsID("stops")); c = xsToInteger(xsGet(xsVar(0), xsID("length"))); if (c > kCanvas2DMaxGradientStops) c = kCanvas2DMaxGradientStops; for (i = 0; i < c; i++) { xsVar(1) = xsGetAt(xsVar(0), xsInteger(i)); stops[i].offset = xsToNumber(xsGet(xsVar(1), xsID("offset"))); xsVar(1) = xsGet(xsVar(1), xsID("color")); if (!KprParseColor(the, xsToString(xsVar(1)), &(stops[i].color))) return; } if (xsTest(xsArg(1))) FskCanvas2dSetStrokeStyleRadialGradient(ctx, x0, y0, r0, x1, y1, r1, c, stops); else FskCanvas2dSetFillStyleRadialGradient(ctx, x0, y0, r0, x1, y1, r1, c, stops); }
static void KprPromiseTargetComplete(KprMessage message, void* it) { KprPromiseTarget self = it; xsBeginHost(self->the); xsVars(3); xsVar(0) = xsAccess(self->resolve); xsVar(1) = xsAccess(self->reject); if (message->error) { xsVar(2) = xsNewInstanceOf(xsGet(xsGet(xsGlobal, xsID_KPR), xsID_message)); xsSetHostData(xsVar(2), message); FskInstrumentedItemSendMessageDebug(message, kprInstrumentedMessageConstruct, message); message->usage++; // host (void)xsCallFunction1(xsVar(1), xsUndefined, xsVar(2)); } else { if (message->stream) KprMessageScriptTargetGet(message, the, &xsVar(2)); else { xsVar(2) = xsNewInstanceOf(xsGet(xsGet(xsGlobal, xsID_KPR), xsID_message)); xsSetHostData(xsVar(2), message); FskInstrumentedItemSendMessageDebug(message, kprInstrumentedMessageConstruct, message); message->usage++; // host } (void)xsCallFunction1(xsVar(0), xsUndefined, xsVar(2)); } xsEndHost(self->the); }
void KPR_canvasPattern_setStyle(xsMachine *the) { FskCanvas2dContext ctx = xsGetHostData(xsArg(0)); UInt32 repetition = kFskCanvas2dPatternRepeat; FskConstBitmap bitmap = NULL; Boolean owned = false; xsVars(1); xsVar(0) = xsGet(xsThis, xsID("repetition")); if (xsTest(xsVar(0))) { xsStringValue it = xsToString(xsVar(0)); if (!FskStrCompare(it, "no-repeat")) repetition = kFskCanvas2dPatternRepeatNone; else if (!FskStrCompare(it, "repeat-x")) repetition = kFskCanvas2dPatternRepeatX; else if (!FskStrCompare(it, "repeat-y")) repetition = kFskCanvas2dPatternRepeatY; else if (!FskStrCompare(it, "repeat")) repetition = kFskCanvas2dPatternRepeat; else xsError(kFskErrInvalidParameter); } xsVar(0) = xsGet(xsThis, xsID("image")); if (xsIsInstanceOf(xsVar(0), xsGet(xsGet(xsGlobal, xsID_KPR), xsID_texture))) { KprTexture texture = xsGetHostData(xsVar(0)); bitmap = KprTextureGetBitmap(texture, NULL, &owned); } else { KprContent content = xsGetHostData(xsVar(0)); bitmap = (*content->dispatch->getBitmap)(content, NULL, &owned); } if (!bitmap) xsError(kFskErrInvalidParameter); if (xsTest(xsArg(1))) FskCanvas2dSetStrokeStylePattern(ctx, repetition, bitmap); else FskCanvas2dSetFillStylePattern(ctx, repetition, bitmap); if (!owned) FskBitmapDispose((FskBitmap)bitmap); }
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 xs_z_mod(xsMachine *the) { z_t *z = xsGetHostData(xsThis); xsVars(1); z_call3(the, z, kcl_z_div, &xsVar(0)); xsResult = xsVar(0); }
void xs_console_load(xsMachine *the) { xsVars(1); xsSetInteger(xsVar(0), CONSOLE_LOG_FILE); xsSet(xsThis, xsID("LOGFILE"), xsVar(0)); xsSetInteger(xsVar(0), CONSOLE_XSBUG); xsSet(xsThis, xsID("XSBUG"), xsVar(0)); }
// drawing images void KPR_canvasRenderingContext2D_drawImage(xsMachine *the) { FskCanvas2dContext ctx = xsGetHostData(xsThis); xsIntegerValue c = xsToInteger(xsArgc); FskConstCanvas cnv = NULL; FskBitmap bitmap = NULL; Boolean owned = false; xsVars(1); if (xsIsInstanceOf(xsArg(0), xsGet(xsGet(xsGlobal, xsID_KPR), xsID_texture))) { KprTexture texture = xsGetHostData(xsArg(0)); bitmap = KprTextureGetBitmap(texture, NULL, &owned); } else { KprContent content = xsGetHostData(xsArg(0)); if (content->dispatch == &KprCanvasDispatchRecord) cnv = ((KprCanvas)content)->cnv; else bitmap = (*content->dispatch->getBitmap)(content, NULL, &owned); } if (!cnv && !bitmap) xsError(kFskErrInvalidParameter); if (c > 8) { xsNumberValue sx = xsToNumber(xsArg(1)); xsNumberValue sy = xsToNumber(xsArg(2)); xsNumberValue sw = xsToNumber(xsArg(3)); xsNumberValue sh = xsToNumber(xsArg(4)); xsNumberValue dx = xsToNumber(xsArg(5)); xsNumberValue dy = xsToNumber(xsArg(6)); xsNumberValue dw = xsToNumber(xsArg(7)); xsNumberValue dh = xsToNumber(xsArg(8)); if (cnv) FskCanvas2dDrawSubScaledCanvas2d(ctx, cnv, sx, sy, sw, sh, dx, dy, dw, dh); else FskCanvas2dDrawSubScaledBitmap(ctx, bitmap, sx, sy, sw, sh, dx, dy, dw, dh); } else if (c > 4) { xsNumberValue dx = xsToNumber(xsArg(1)); xsNumberValue dy = xsToNumber(xsArg(2)); xsNumberValue dw = xsToNumber(xsArg(3)); xsNumberValue dh = xsToNumber(xsArg(4)); if (cnv) FskCanvas2dDrawScaledCanvas2d(ctx, cnv, dx, dy, dw, dh); else FskCanvas2dDrawScaledBitmap(ctx, bitmap, dx, dy, dw, dh); } else { xsNumberValue dx = xsToNumber(xsArg(1)); xsNumberValue dy = xsToNumber(xsArg(2)); if (cnv) FskCanvas2dDrawCanvas2d(ctx, cnv, dx, dy); else FskCanvas2dDrawBitmap(ctx, bitmap, dx, dy); } if (!owned) FskBitmapDispose(bitmap); }
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)); }
void KPR_system_getWifiInfo(xsMachine* the) { DWORD dwResult = 0; HANDLE hClient = NULL; DWORD dwMaxClient = 2; DWORD dwCurVersion = 0; PWLAN_INTERFACE_INFO_LIST pIfList = NULL; int i; PWLAN_INTERFACE_INFO pIfInfo = NULL; DWORD connectInfoSize = sizeof(WLAN_CONNECTION_ATTRIBUTES); PWLAN_CONNECTION_ATTRIBUTES pConnectInfo = NULL; WLAN_OPCODE_VALUE_TYPE opCode = wlan_opcode_value_type_invalid; ULONG length; xsVars(1); dwResult = WlanOpenHandle(dwMaxClient, NULL, &dwCurVersion, &hClient); if (dwResult != ERROR_SUCCESS) goto bail; dwResult = WlanEnumInterfaces(hClient, NULL, &pIfList); if (dwResult != ERROR_SUCCESS) goto bail; for (i = 0; i < (int) pIfList->dwNumberOfItems; i++) { pIfInfo = (WLAN_INTERFACE_INFO *) &pIfList->InterfaceInfo[i]; if (pIfInfo->isState == wlan_interface_state_connected) { dwResult = WlanQueryInterface(hClient, &pIfInfo->InterfaceGuid, wlan_intf_opcode_current_connection, NULL, &connectInfoSize, (PVOID *) &pConnectInfo, &opCode); if (dwResult != ERROR_SUCCESS) goto bail; length = pConnectInfo->wlanAssociationAttributes.dot11Ssid.uSSIDLength; if (length > 0) { xsResult = xsNewInstanceOf(xsObjectPrototype); xsVar(0) = xsStringBuffer(NULL, length + 1); FskMemCopy(xsToString(xsVar(0)), pConnectInfo->wlanAssociationAttributes.dot11Ssid.ucSSID, length); xsSet(xsResult, xsID("SSID"), xsVar(0)); } break; } } bail: if (pConnectInfo != NULL) { WlanFreeMemory(pConnectInfo); pConnectInfo = NULL; } if (pIfList != NULL) { WlanFreeMemory(pIfList); pIfList = NULL; } if (hClient != NULL) { WlanCloseHandle(hClient, NULL); hClient = NULL; } }
FskErr FskSSLNew(void **fsslp, const char *host, int port, Boolean blocking, long flags, int priority) { FskSSL *fssl; FskErr err; if ((err = FskMemPtrNewClear(sizeof(FskSSL), (FskMemPtr*)(void*)&fssl)) != kFskErrNone) return err; if ((err = newSSLVM(&fssl->vm)) != kFskErrNone) { FskMemPtrDispose(fssl); return err; } xsBeginHost(fssl->vm->the); xsTry { const char *prStr; xsVars(2); /* construct the options */ xsVar(0) = xsNewInstanceOf(xsObjectPrototype); if (blocking) xsSet(xsVar(0), xsID("blocking"), xsTrue); if (flags & kConnectFlagsSynchronous) xsSet(xsVar(0), xsID("synchronous"), xsTrue); switch (priority) { default: case kFskNetSocketLowestPriority: prStr = "lowest"; break; case kFskNetSocketLowPriority: prStr = "low"; break; case kFskNetSocketMediumPriority: prStr = "medium"; break; case kFskNetSocketHighPriority: prStr = "high"; break; case kFskNetSocketHighestPriority: prStr = "highest"; break; } (void)xsSet(xsVar(0), xsID("priority"), xsString((xsStringValue)prStr)); (void)xsSet(xsVar(0), xsID("raw"), xsTrue); xsVar(1) = xsNew3(xsGet(xsGlobal, xsID("Stream")), xsID("Socket"), xsString((xsStringValue)host), xsInteger(port), xsVar(0)); fssl->socket = xsVar(1); xsRemember(fssl->socket); xsVar(1) = xsNew0(xsGet(xsGlobal, xsID("FskSSL")), xsID("Session")); fssl->ssl = xsVar(1); xsRemember(fssl->ssl); } xsCatch { if (xsHas(xsException, xsID("code"))) err = xsToInteger(xsGet(xsException, xsID("code"))); if (err == kFskErrNone) err = kFskErrOperationFailed; } xsEndHost(fssl->vm->the); if (err == kFskErrNone) { if (fsslp != NULL) *fsslp = fssl; } else { disposeSSLVM(fssl->vm); FskMemPtrDispose(fssl); } return err; }
void xs_z_div2(xsMachine *the) { z_t *z = xsGetHostData(xsThis); xsVars(2); z_call3(the, z, kcl_z_div, &xsVar(1)); xsVar(0) = xsResult; xsResult = xsNewInstanceOf(xsObjectPrototype); xsSet(xsResult, xsID("q"), xsVar(0)); xsSet(xsResult, xsID("r"), xsVar(1)); }
void KPR_system_alert(xsMachine* the) { int argc = xsToInteger(xsArgc); MSGBOXPARAMSW params; xsStringValue string; xsIntegerValue result; xsVars(1); params.cbSize = sizeof(params); params.hwndOwner = NULL; params.hInstance = FskMainGetHInstance(); params.lpszText = NULL; params.lpszCaption = xsStringToWideString("Kinoma Code"); params.dwStyle = MB_ICONSTOP; params.dwContextHelpId = 0; params.lpfnMsgBoxCallback = NULL; params.dwLanguageId = MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT); if ((argc > 0) && xsTest(xsArg(0))) { if (xsFindString(xsArg(0), xsID_type, &string)) { if (!FskStrCompare(string, "about")) params.dwStyle = MB_ICONINFORMATION; else if (!FskStrCompare(string, "stop")) params.dwStyle = MB_ICONSTOP; else if (!FskStrCompare(string, "note")) params.dwStyle = MB_ICONEXCLAMATION; } if (xsFindResult(xsArg(0), xsID_prompt)) { xsVar(0) = xsResult; } if (xsFindResult(xsArg(0), xsID_info)) { xsVar(0) = xsCall1(xsVar(0), xsID("concat"), xsString("\n\n")); xsVar(0) = xsCall1(xsVar(0), xsID("concat"), xsResult); } params.lpszText = xsStringToWideString(xsToString(xsVar(0))); if (xsFindResult(xsArg(0), xsID_buttons)) { if (xsIsInstanceOf(xsResult, xsArrayPrototype)) { xsIntegerValue c = xsToInteger(xsGet(xsResult, xsID_length)); if (c == 3) params.dwStyle |= MB_YESNOCANCEL; else if (c == 2) params.dwStyle |= MB_OKCANCEL; else params.dwStyle |= MB_OK; } } } result = MessageBoxIndirectW(¶ms); if (params.lpszText) CoTaskMemFree((LPVOID *)params.lpszText); if (params.lpszCaption) CoTaskMemFree((LPVOID *)params.lpszCaption); if ((argc > 1) && xsTest(xsArg(1))) (void)xsCallFunction1(xsArg(1), xsNull, ((result == IDYES) || (result == IDOK)) ? xsTrue : (result == IDNO) ? xsFalse : xsUndefined); }
void xs_system_get_timezone(xsMachine *the) { struct timezone tz; xsVars(1); mc_gettimeofday(NULL, &tz); xsResult = xsNewInstanceOf(xsObjectPrototype); xsSetInteger(xsVar(0), tz.tz_minuteswest); xsSet(xsResult, xsID("timedifference"), xsVar(0)); xsSetInteger(xsVar(0), tz.tz_dsttime); xsSet(xsResult, xsID("dst"), xsVar(0)); }
void Zeroconf_browser_callback(KprZeroconfBrowser self, char* function, KprZeroconfServiceInfo service) { xsBeginHostSandboxCode(self->the, self->code); if (xsTypeOf(self->behavior) == xsUndefinedType) goto bail; xsVars(3); { xsTry { xsVar(0) = xsAccess(self->behavior); xsVar(1) = xsNewInstanceOf(xsObjectPrototype); xsSet(xsVar(1), xsID("name"), xsString(service->name)); xsSet(xsVar(1), xsID("type"), xsString(service->type)); if (service->host) xsSet(xsVar(1), xsID("host"), xsString(service->host)); if (service->ip) { xsSet(xsVar(1), xsID("ip"), xsString(service->ip)); xsSet(xsVar(1), xsID("port"), xsInteger(service->port)); } if (service->txt) { char* txt = service->txt; UInt32 position = 0, size = FskStrLen(txt); UInt32 length = 0; xsVar(2) = xsNewInstanceOf(xsObjectPrototype); xsSet(xsVar(1), xsID("txt"), xsVar(2)); length = txt[position++] & 0xFF; while ((position + length) <= size) { char end; char* equal; if (!length) break; end = txt[position + length]; txt[position + length] = 0; equal = FskStrChr(txt + position, '='); if (equal) { *equal = 0; xsSet(xsVar(2), xsID(txt + position), xsString(equal + 1)); *equal = '='; } txt[position + length] = end; position += length; length = txt[position++] & 0xFF; } } if (xsFindResult(xsVar(0), xsID(function))) { (void)xsCallFunction1(xsResult, xsVar(0), xsVar(1)); } } xsCatch { } } bail: xsEndHostSandboxCode(); }
static void usb_callback(xsMachine *the, void *closure) { char *buf = closure; xsBeginHost(the); xsVars(2); xsGet(xsVar(0), xsGlobal, xsID("require")); xsSetString(xsVar(1), "CLI"); xsCall(xsResult, xsVar(0), xsID("weak"), &xsVar(1), NULL); xsSetString(xsVar(1), buf); xsCall_noResult(xsResult, xsID("evaluate"), &xsVar(1), NULL); xsEndHost(the); }
void xs_infoset_parse(xsMachine* the) { xsVars(COUNT); xsVar(ATTRIBUTE_PROTOTYPE) = xsGet(xsThis, xsID_attribute); xsVar(CDATA_PROTOTYPE) = xsGet(xsThis, xsID_cdata); xsVar(COMMENT_PROTOTYPE) = xsGet(xsThis, xsID_comment); xsVar(DOCUMENT_PROTOTYPE) = xsGet(xsThis, xsID_document); xsVar(ELEMENT_PROTOTYPE) = xsGet(xsThis, xsID_element); if ((xsToInteger(xsArgc) < 1) || (!xsIsInstanceOf(xsArg(0), xsVar(DOCUMENT_PROTOTYPE)))) xsTypeError("document is no infoset.document"); xsVar(RULE_SET) = xsGet(xsGet(xsGlobal, xsID_Grammar), xsID_ruleSet); xsVar(CHILD) = xsGet(xsArg(0), xsID_element); parseElement(the); }
void xs_tftpd_start(xsMachine *the) { int s; void *instance; xsVars(1); xsGet(xsVar(0), xsArg(0), xsID("nativeSocket")); s = xsToInteger(xsVar(0)); instance = tftpd_connect(s, the, &xsThis); if (instance == NULL) mc_xs_throw(the, "tftpd: no mem"); xsSetHostData(xsThis, instance); }
void xs_ed_add(xsMachine *the) { ed_t *ed = xsGetHostData(xsThis); kcl_ecp_t *a, *b, *r; kcl_err_t err; xsVars(1); a = arith_get_ecp(ed, xsArg(0)); b = arith_get_ecp(ed, xsArg(1)); (void)((err = kcl_ecp_alloc(&r)) || (err = kcl_ed_add(ed->ctx, a, b, r))); if (err != KCL_ERR_NONE) kcl_throw_error(the, err); arith_set_ecp(ed, xsResult, r); }
void xs_system_set_timezone(xsMachine *the) { struct timezone tz; xsVars(1); xsGet(xsVar(0), xsArg(0), xsID("timedifference")); tz.tz_minuteswest = xsToInteger(xsVar(0)); xsGet(xsVar(0), xsArg(0), xsID("dst")); tz.tz_dsttime = xsToInteger(xsVar(0)); mc_settimeofday(NULL, &tz); if (xsHas(xsArg(0), xsID("timezone"))) { xsGet(xsVar(0), xsArg(0), xsID("timezone")); mc_env_set_default("TIME_ZONE", xsToString(xsVar(0))); } }
void xs_ed_mul(xsMachine *the) { ed_t *ed = xsGetHostData(xsThis); kcl_ecp_t *p, *r; kcl_int_t *k; kcl_err_t err; xsVars(1); p = arith_get_ecp(ed, xsArg(0)); k = arith_get_integer(ed, xsArg(1)); (void)((err = kcl_ecp_alloc(&r)) || (err = kcl_ed_mul(ed->ctx, p, k, r))); if (err != KCL_ERR_NONE) kcl_throw_error(the, err); arith_set_ecp(ed, xsResult, r); }
void KPR_shell_execute_stdout(KprShellExec exec, xsStringValue string) { if (string) { xsBeginHost(exec->the); xsVars(3); xsVar(0) = xsAccess(exec->slot); xsVar(1) = xsGet(xsVar(0), xsID_stdout); if (xsTest(xsVar(1))) { xsVar(2) = xsString(string); xsResult = xsCallFunction1(xsVar(1), xsNull, xsVar(2)); } xsEndHost(exec->the); FskMemPtrDispose(string); } KPR_shell_execute_callback(exec); }
void xs_tftpd_connect(xsMachine *the) { int s; void *instance; xsVars(1); xsGet(xsVar(0), xsArg(0), xsID("nativeSocket")); s = xsToInteger(xsVar(0)); instance = tftpd_connect(s, the, &xsThis); if (instance == NULL) { xsSetBoolean(xsResult, 0); return; } xsSetHostData(xsThis, instance); xsSetBoolean(xsResult, 1); }
void KPR_shell_execute_callback(KprShellExec exec) { exec->usage--; if (exec->usage == 0) { xsBeginHost(exec->the); xsVars(3); xsVar(0) = xsAccess(exec->slot); xsVar(1) = xsGet(xsVar(0), xsID_callback); if (xsTest(xsVar(1))) { xsVar(2) = xsInteger(exec->status); xsResult = xsCallFunction1(xsVar(1), xsNull, xsVar(2)); } xsForget(exec->slot); xsSetHostData(xsVar(0), NULL); xsEndHost(exec->the); FskMemPtrDispose(exec); } }
void Zeroconf_advertisement_callback(KprZeroconfAdvertisement self, char* function) { xsBeginHostSandboxCode(self->the, self->code); if (xsTypeOf(self->behavior) == xsUndefinedType) goto bail; xsVars(2); { xsTry { xsVar(0) = xsAccess(self->behavior); xsVar(1) = xsAccess(self->slot); if (xsFindResult(xsVar(0), xsID(function))) { (void)xsCallFunction1(xsResult, xsVar(0), xsVar(1)); } } xsCatch { } } bail: xsEndHostSandboxCode(); }
FskErr FskSSLLoadCerts(void *a, FskSocketCertificateRecord *cert) { FskSSL *fssl = a; FskErr err = kFskErrNone; xsBeginHost(fssl->vm->the); xsTry { xsVars(2); xsVar(0) = xsUndefined; xsVar(1) = xsUndefined; if (cert != NULL) { if (cert->certificates != NULL && cert->certificatesSize > 0) { xsVar(0) = xsNew1(xsGlobal, xsID("Chunk"), xsInteger(cert->certificatesSize)); FskMemCopy(xsGetHostData(xsVar(0)), cert->certificates, cert->certificatesSize); } if (cert->policies != NULL) { if (cert->certificates == NULL) xsVar(0) = xsNew0(xsGlobal, xsID("Chunk")); // create a null chunk just for setting the polices xsSet(xsVar(0), xsID("policies"), xsString(cert->policies)); } if (cert->hostname != NULL) { xsSet(fssl->socket, xsID("hostname"), xsString(cert->hostname)); // too easy but should work... } if (cert->key != NULL && cert->keySize > 0) { xsVar(1) = xsNew1(xsGlobal, xsID("Chunk"), xsInteger(cert->keySize)); FskMemCopy(xsGetHostData(xsVar(1)), cert->key, cert->keySize); } } xsCall2_noResult(fssl->ssl, xsID("loadCerts"), xsVar(0), xsVar(1)); } xsCatch { if (xsHas(xsException, xsID("code"))) err = xsToInteger(xsGet(xsException, xsID("code"))); if (err == kFskErrNone) err = kFskErrOperationFailed; } xsEndHost(fssl->vm->the); return err; }
void xs_system_init(xsMachine *the) { int i, j; xsSet(xsThis, xsID("_global"), xsGlobal); mc_rng_init(NULL, 0); /* * set config */ if (mc_conf.deviceID == NULL) return; /* nothing to set */ xsVars(4); xsGet(xsVar(0), xsThis, xsID("_config")); /* LEDs */ xsSetNewInstanceOf(xsVar(1), xsArrayPrototype); for (i = 0, j = 0; i < MC_MAX_LED_PINS; i++) { if (mc_conf.led_pins[i] >= 0) { xsSetInteger(xsVar(2), j); j++; xsSetInteger(xsVar(3), mc_conf.led_pins[i]); xsSetAt(xsVar(1), xsVar(2), xsVar(3)); } } if (j > 0) xsSet(xsVar(0), xsID("ledPins"), xsVar(1)); /* wakeup buttons */ xsSetNewInstanceOf(xsVar(1), xsArrayPrototype); for (i = 0, j = 0; i < MC_MAX_WAKEUP_BUTTONS; i++) { if (mc_conf.wakeup_buttons[i] >= 0) { xsSetInteger(xsVar(2), j); j++; xsSetInteger(xsVar(3), mc_conf.wakeup_buttons[i]); xsSetAt(xsVar(1), xsVar(2), xsVar(3)); } } if (j > 0) xsSet(xsVar(0), xsID("wakeupButtons"), xsVar(1)); /* power/ground enable */ xsSetBoolean(xsVar(1), mc_conf.power_ground_pinmux); xsSet(xsVar(0), xsID("powerGroundPinmux"), xsVar(1)); xsSetBoolean(xsVar(1), mc_conf.usb_console); xsSet(xsVar(0), xsID("usbConsole"), xsVar(1)); }