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 kpr2jsSplitPath(xsMachine* the) { char *aPath; char *aSeparator = NULL; char *aDot = NULL; int aLength; char aDirectory[1024]; char aName[1024]; char anExtension[1024]; aPath = xsToString(xsArg(0)); aSeparator = strrchr(aPath, mxSeparator); if (aSeparator == NULL) aSeparator = aPath; else aSeparator++; aDot = strrchr(aSeparator, '.'); if (aDot == NULL) aDot = aSeparator + strlen(aSeparator); aLength = aSeparator - aPath; strncpy(aDirectory, aPath, aLength); aDirectory[aLength - 1] = 0; aLength = aDot - aSeparator; strncpy(aName, aSeparator, aLength); aName[aLength] = 0; strcpy(anExtension, aDot); xsResult = xsNewInstanceOf(xsArrayPrototype); xsSet(xsResult, 0, xsString(aDirectory)); xsSet(xsResult, 1, xsString(aName)); xsSet(xsResult, 2, xsString(anExtension)); }
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)); }
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; }
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; }
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 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 scanComment(void *data, const char *text) { Scanner* self = data; xsMachine* the = self->the; scanText(data); xsVar(VALUE) = xsString((xsStringValue)text); xsVar(LINE) = xsInteger(XML_GetCurrentLineNumber(self->expat)); xsVar(CHILD) = xsNewInstanceOf(xsVar(COMMENT_PROTOTYPE)); xsSet(xsVar(CHILD), xsID_path, xsVar(PATH)); xsSet(xsVar(CHILD), xsID_line, xsVar(LINE)); xsSet(xsVar(CHILD), xsID_parent, xsResult); xsSet(xsVar(CHILD), xsID_value, xsVar(VALUE)); xsArrayCacheItem(xsVar(CHILDREN), xsVar(CHILD)); }
void scanText(void *data) { Scanner* self = data; xsMachine* the = self->the; if (self->textOffset) { self->textBuffer[self->textOffset] = 0; xsVar(VALUE) = xsString(self->textBuffer); self->textOffset = 0; xsVar(CHILD) = xsNewInstanceOf(xsVar(CDATA_PROTOTYPE)); xsSet(xsVar(CHILD), xsID_path, xsVar(PATH)); xsSet(xsVar(CHILD), xsID_line, xsVar(LINE)); xsSet(xsVar(CHILD), xsID_parent, xsResult); xsSet(xsVar(CHILD), xsID_value, xsVar(VALUE)); xsArrayCacheItem(xsVar(CHILDREN), xsVar(CHILD)); } }
void kpr2jsWriteFile(xsMachine* the) { FILE* aFile = xsGetHostData(xsThis); int c = fprintf(aFile, "%s", xsToString(xsArg(0))); c += xsToInteger(xsGet(xsThis, xsID("charCount"))); xsSet(xsThis, xsID("charCount"), xsInteger(c)); }
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(); }
void KPR_imageData_get_data(xsMachine *the) { FskCanvas2dImageData data = xsGetHostData(xsThis); xsResult = xsNewInstanceOf(xsChunkPrototype); xsSetHostData(xsResult, (void*)data->data.bytes); xsSetHostDestructor(xsResult, NULL); xsSet(xsResult, xsID("length"), xsInteger(data->data.length)); }
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 xs_i2c_readBlock(xsMachine *the) { FskErr err; xsI2C i2c = xsGetHostData(xsThis); int argc = xsToInteger(xsArgc), i; int format = 2; SInt32 dataSize = xsToInteger(xsArg(0)), readCount; UInt8 data[32]; xsThrowIfNULL(i2c); DBG_I2C("xs_i2c_readBlock\n"); if ((dataSize > 32) || (dataSize <= 0)) xsThrowDiagnosticIfFskErr(kFskErrInvalidParameter, "I2C readBlock invalid size %d. %s", (int)dataSize, i2c->diagnosticID); FskPinI2CSetAddress(i2c->pin, i2c->address); err = FskPinI2CReadBytes(i2c->pin, dataSize, &readCount, data); if (err) { xsTraceDiagnostic("I2C readBlock failed with error %s %s.", FskInstrumentationGetErrorString(err), i2c->diagnosticID); goto bail; } if (argc > 1) { int t = xsTypeOf(xsArg(1)); if ((xsNumberType == t) || (t == xsIntegerType)) format = xsToInteger(xsArg(1)); else { char *formatString = xsToString(xsArg(1)); if (0 == FskStrCompare(formatString, "Buffer")) format = 2; else if (0 == FskStrCompare(formatString, "Chunk")) format = 0; else if (0 == FskStrCompare(formatString, "Array")) format = 1; } } if (2 == format) { xsResult = xsArrayBuffer(data, readCount); } else if (0 == format) { xsResult = xsNew1(xsGlobal, xsID("Chunk"), xsInteger(readCount)); FskMemMove(xsGetHostData(xsResult), data, readCount); } else if (1 == format) { xsResult = xsNew1(xsGlobal, xsID("Array"), xsInteger(readCount)); for (i = 0; i < readCount; i++) xsSet(xsResult, i, xsInteger(data[i])); } bail: if (err) xsError(err); }
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; } }
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)); }
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 KPR_canvas_getContext(xsMachine *the) { KprCanvas self = xsGetHostData(xsThis); if (self->cnv) { FskCanvas2dContext ctx = FskCanvasGet2dContext(self->cnv); xsResult = xsNewInstanceOf(xsGet(xsGet(xsGlobal, xsID("KPR")), xsID("canvasRenderingContext2D"))); xsSetHostData(xsResult, ctx); xsSet(xsResult, xsID("canvas"), xsThis); KprContentInvalidate((KprContent)self); } }
void xsMemPtrToChunk(xsMachine *the, xsSlot *ref, FskMemPtr data, UInt32 dataSize, Boolean alreadyAllocated) { xsDestructor destructor; if (!alreadyAllocated) *ref = xsNewInstanceOf(xsChunkPrototype); xsSetHostData(*ref, data); destructor = xsGetHostDestructor(*ref); xsSetHostDestructor(*ref, NULL); xsSet(*ref, xsID_length, xsInteger(dataSize)); xsSetHostDestructor(*ref, destructor); }
void xs_dbg_report(xsMachine *the) { if (xsToInteger(xsArgc) > 0 && xsTest(xsArg(0))) { /* silence - return the memory use */ xsMemoryUse slot, chunk; xsReportMemoryUse(the, &slot, &chunk); xsVars(1); xsResult = xsNewInstanceOf(xsObjectPrototype); xsSetInteger(xsVar(0), chunk.current); xsSet(xsResult, xsID("chunk"), xsVar(0)); xsSetInteger(xsVar(0), slot.current); xsSet(xsResult, xsID("slot"), xsVar(0)); } else { #if mxMC mc_mstats(true); #endif xsReportMemoryUse(the, NULL, NULL); } }
void KPR_canvasRenderingContext2D_measureText(xsMachine *the) { FskCanvas2dContext ctx = xsGetHostData(xsThis); xsStringValue text = xsToString(xsArg(0)); UInt16* buffer; if (kFskErrNone == FskTextUTF8ToUnicode16NE((unsigned char*)text, FskStrLen(text), &buffer, NULL)) { xsNumberValue width = FskCanvas2dMeasureText(ctx, buffer); FskMemPtrDispose(buffer); xsResult = xsNewInstanceOf(xsGet(xsGet(xsGlobal, xsID("KPR")), xsID("textMetrics"))); xsSet(xsResult, xsID("width"), xsNumber(width)); } }
xsBooleanValue fxFindModuleLoadAsDirectory(xsMachine* the, xsStringValue base, xsStringValue name, xsIndex* id) { char node[1024]; UInt32 nodeSize; xsStringValue slash; FskErr err; FskFileMapping map = NULL; xsBooleanValue result = 0; xsStringValue path = NULL; unsigned char* data; FskInt64 size; xsSlot slot; nodeSize = sizeof(node); FskStrNCopy(node, base, nodeSize); slash = FskStrRChr(node, '/'); if (slash) *slash = 0; FskStrNCat(node, "/", nodeSize); FskStrNCat(node, name, nodeSize); if (FskStrTail(node, "/") != 0) FskStrNCat(node, "/", nodeSize); FskStrNCat(node, "package.json", nodeSize); err = KprURLToPath(node, &path); if (err == kFskErrNone) { err = FskFileMap(path, &data, &size, 0, &map); if (err == kFskErrNone) { xsTry { slot = xsNewInstanceOf(xsChunkPrototype); xsSetHostData(slot, data); xsSetHostDestructor(slot, NULL); xsSet(slot, xsID("length"), xsInteger(size)); slot = xsCall1(xsGet(xsGlobal, xsID("JSON")), xsID("parse"), slot); } xsCatch { slot = xsUndefined; } if (xsTest(slot)) { slot = xsGet(slot, xsID("main")); if (xsTest(slot)) { FskStrNCopy(node, name, nodeSize); if (FskStrTail(node, "/") != 0) FskStrNCat(node, "/", nodeSize); FskStrNCat(node, xsToString(slot), nodeSize); if (fxFindModuleLoadAsFile(the, base, node, id)) result = 1; } } }
int main(int argc, char* argv[]) { xsAllocation anAllocation = { 2048 * 1024, /* initialChunkSize */ 512 * 1024, /* incrementalChunkSize */ 1000 * 1000, /* initialHeapCount */ 50 * 1000, /* incrementalHeapCount */ 1024, /* stackCount */ 8192, /* symbolCount */ 1993 /* symbolModulo */ }; xsMachine* aMachine; int argi; int result = 1; for (argi = 0; argi < argc; argi++){ result = strncmp(argv[argi], "--help", strlen("--help")); if(!result) { printHelp(); exit(0); } } result = 1; gxProperties = calloc(mxPropertyModulo, sizeof(xsSlot)); if (gxProperties) { aMachine = xsNewMachine(&anAllocation, &kprconfigGrammar, NULL); if (aMachine) { xsBeginHost(aMachine); { xsTry { xsResult = xsNewInstanceOf(xsArrayPrototype); for (argi = 0; argi < argc; argi++) xsSet(xsResult, argi, xsString(argv[argi])); (void)xsCall1(xsGlobal, xsID("main"), xsResult); result = 0; } xsCatch { char* aMessage = xsToString(xsGet(xsException, xsID("message"))); fprintf(stderr, "### %s\n", aMessage); } } xsEndHost(aMachine); xsDeleteMachine(aMachine); } else fprintf(stderr, "### Cannot allocate machine!\n"); }
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(); }
void xs_timeInterval_constructor(xsMachine *the) { mc_timeinterval_t *ti; unsigned long interval; if ((ti = mc_malloc(sizeof(mc_timeinterval_t))) == NULL) mc_xs_throw(the, "TimeInteval: no mem"); interval = xsToInteger(xsArg(1)); xsSet(xsThis, xsID("_callback"), xsArg(0)); ti->obj = xsThis; if ((ti->tc = mc_interval_set(interval, timeInterval_callback, ti)) == NULL) { xs_timeInterval_destructor(ti); mc_xs_throw(the, "TimeInterval: no time slot"); } xsSetHostData(xsThis, ti); }
void kpr2jsReportWarning(xsMachine* the) { char* aPath; long aLine; int aCount; if (xsTypeOf(xsArg(0)) == xsStringType) { aPath = xsToString(xsArg(0)); aLine = xsToInteger(xsArg(1)); #if mxWindows fprintf(stderr, "%s(%ld): warning: ", aPath, aLine); #else fprintf(stderr, "%s:%ld: warning: ", aPath, aLine); #endif } fprintf(stderr, "%s!\n", xsToString(xsArg(2))); aCount = xsToInteger(xsGet(xsThis, xsID("warningCount"))); xsSet(xsThis, xsID("warningCount"), xsInteger(aCount + 1)); }
void xsToolInsertProperty(xsMachine* the) { xsStringValue aString; xsIntegerValue aSum; aString = xsToString(xsArg(0)); aSum = 0; while(*aString != 0) { aSum = (aSum << 1) + *aString++; } aSum &= 0x7FFFFFFF; xsResult = gxProperties[aSum % mxPropertyModulo]; if (xsTypeOf(xsResult) == xsReferenceType) (void)xsCall3(xsResult, xsID("insertProperty"), xsArg(0), xsArg(1), xsArg(2)); else { gxProperties[aSum % mxPropertyModulo] = xsArg(1); xsSet(xsArg(1), xsID("qualifiedName"), xsArg(0)); } }
void scanProcessingInstruction(void *data, const char *target, const char *text) { Scanner* self = data; xsMachine* the = self->the; scanText(data); scanName(the, target, 1); xsVar(VALUE) = xsString((xsStringValue)text); xsVar(LINE) = xsInteger(XML_GetCurrentLineNumber(self->expat)); xsVar(CHILD) = xsNewInstanceOf(xsVar(PI_PROTOTYPE)); xsSet(xsVar(CHILD), xsID_path, xsVar(PATH)); xsSet(xsVar(CHILD), xsID_line, xsVar(LINE)); xsSet(xsVar(CHILD), xsID_parent, xsResult); xsSet(xsVar(CHILD), xsID_name, xsVar(NAME)); xsSet(xsVar(CHILD), xsID_namespace, xsVar(NAMESPACE)); xsSet(xsVar(CHILD), xsID_prefix, xsVar(PREFIX)); xsSet(xsVar(CHILD), xsID_value, xsVar(VALUE)); xsArrayCacheItem(xsVar(CHILDREN), xsVar(CHILD)); }
FskErr FskSSLAttach(void **fsslp, FskSocket skt) { 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 { xsVars(2); xsVar(0) = xsNew1(xsGet(xsGlobal, xsID("Stream")), xsID("Socket"), xsHostData(skt)); xsVar(1) = xsNewInstanceOf(xsObjectPrototype); (void)xsSet(xsVar(1), xsID("raw"), xsTrue); xsCall1_noResult(xsVar(0), xsID("setProperties"), xsVar(1)); fssl->socket = xsVar(0); xsRemember(fssl->socket); xsVar(0) = xsNew0(xsGet(xsGlobal, xsID("FskSSL")), xsID("Session")); fssl->ssl = xsVar(0); 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_i2c_readBlockDataSMB(xsMachine* the) { xsI2C i2c = xsGetHostData(xsThis); FskErr err; char* formatString; int format = 2, i; SInt32 dataSize = 0; UInt8 command = (UInt8)xsToInteger(xsArg(0)); UInt8 data[34]; //needs to be 34 because we're using I2C_SMBUS_I2C_BLOCK_BROKEN in i2cdev.c //@@ WTF - not at this layer, at least SInt32 length = (SInt32)xsToInteger(xsArg(1)); DBG_I2C("xs_i2c_readBlockDataSMB\n"); xsThrowIfNULL(i2c); if ((length < 0) || (length > 32)) xsThrowDiagnosticIfFskErr(kFskErrInvalidParameter, "I2C readBlockDataSMB bad length %d %s.", length, i2c->diagnosticID); FskPinI2CSetAddress(i2c->pin, i2c->address); formatString = xsToString(xsArg(2)); if (!FskStrCompare(formatString, "Buffer")) format = 2; else if (!FskStrCompare(formatString, "Chunk")) format = 0; else if (!FskStrCompare(formatString, "Array")) format = 1; err = FskPinI2CReadDataBytes(i2c->pin, command, length, &dataSize, data); xsThrowDiagnosticIfFskErr(err, "I2C readBlockDataSMB register %d failed %s.", (int)command, i2c->diagnosticID); if (2 == format) { xsResult = xsArrayBuffer(data, dataSize); } else if (0 == format) { xsResult = xsNew1(xsGlobal, xsID("Chunk"), xsInteger(dataSize)); FskMemMove(xsGetHostData(xsResult), data, dataSize); } else if (1 == format) { xsResult = xsNew1(xsGlobal, xsID("Array"), xsInteger(dataSize)); for (i = 0; i < dataSize; i++) xsSet(xsResult, i, xsInteger(data[i])); } }