void xs_env_set(xsMachine *the) { mc_env_t *env; char *val, *name; int pos; int argc = xsToInteger(xsArgc); if (argc < 1) return; env = xsGetHostData(xsThis); if (1 == argc || xsTypeOf(xsArg(1)) == xsUndefinedType) { int result; name = xsToString(xsArg(0)); result = mc_env_unset(env, name); xsSetBoolean(xsResult, result == 0); } else { if ((val = mc_strdup(xsToString(xsArg(1)))) == NULL) return; pos = argc > 2 && xsTypeOf(xsArg(2)) != xsUndefinedType ? xsToInteger(xsArg(2)) : -1; name = xsToString(xsArg(0)); mc_env_set(env, name, val, pos); mc_free(val); } }
void xs_stream_encrypt(xsMachine *the) { crypt_stream_t *stream = xsGetHostData(xsThis); int ac = xsToInteger(xsArgc); size_t len; void *indata, *outdata; if (xsTypeOf(xsArg(0)) == xsStringType) len = strlen(xsToString(xsArg(0))); else len = xsGetArrayBufferLength(xsArg(0)); if (ac > 2 && xsTypeOf(xsArg(2)) != xsUndefinedType) { size_t n = xsToInteger(xsArg(2)); if (n < len) len = n; } xsResult = ac > 1 && xsTest(xsArg(1)) ? xsArg(1) : xsArrayBuffer(NULL, len); if (xsTypeOf(xsArg(0)) == xsStringType) indata = xsToString(xsArg(0)); else indata = xsToArrayBuffer(xsArg(0)); outdata = xsToArrayBuffer(xsResult); (*stream->process)(stream->ctx, indata, outdata, len); }
void KPR_debug_file(xsMachine *the) { KprDebug self = xsGetHostData(xsThis); KprDebugMachine machine = NULL; int command = mxNoCommand; char* path = NULL; char* value = NULL; xsIntegerValue line = -1; xsIntegerValue c = xsToInteger(xsArgc); xsVars(4); if ((c >= 2) && (xsTypeOf(xsArg(0)) == xsStringType) && (xsTypeOf(xsArg(1)) == xsIntegerType)) { char* address = xsToString(xsArg(0)); command = xsToInteger(xsArg(1)); machine = KprDebugFindMachine(self, address); } if ((c >= 4) && (xsTypeOf(xsArg(2)) == xsStringType) && (xsTypeOf(xsArg(3)) == xsIntegerType)) { path = FskStrDoCopy(xsToString(xsArg(2))); line = xsToInteger(xsArg(3)); } if ((c >= 5) && (xsTypeOf(xsArg(4)) == xsStringType)) { value = FskStrDoCopy(xsToString(xsArg(4))); } if (machine && ((command == mxFramesView) || (command == mxFilesView) || (command == mxLogView) || (command == mxBreakpointsView))) { xsEnterSandbox(); KprDebugMachineDispatchCommand(machine, command, path, value, line); xsLeaveSandbox(); } else xsThrowIfFskErr(kFskErrInvalidParameter); FskMemPtrDispose(value); FskMemPtrDispose(path); }
void xs_stream_encrypt(xsMachine *the) { crypt_stream_t *stream = xsGetHostData(xsThis); int ac = xsToInteger(xsArgc); size_t len; void *indata, *outdata; if (xsTypeOf(xsArg(0)) == xsStringType) len = c_strlen(xsToString(xsArg(0))); else len = xsGetArrayBufferLength(xsArg(0)); if (ac > 2 && xsTypeOf(xsArg(2)) != xsUndefinedType) { size_t n = xsToInteger(xsArg(2)); if (n < len) len = n; } if (ac > 1 && xsTest(xsArg(1))) { if (xsGetArrayBufferLength(xsArg(1)) < (xsIntegerValue)len) crypt_throw_error(the, "too small buffer"); xsResult = xsArg(1); } else xsResult = xsArrayBuffer(NULL, len); if (xsTypeOf(xsArg(0)) == xsStringType) indata = xsToString(xsArg(0)); else indata = xsToArrayBuffer(xsArg(0)); outdata = xsToArrayBuffer(xsResult); (*stream->process)(stream->ctx, indata, outdata, len); }
void xs_system_init_rng(xsMachine *the) { size_t sz; void *data; switch (xsTypeOf(xsArg(0))) { case xsIntegerType: case xsNumberType: { unsigned long n = xsToNumber(xsArg(0)); data = &n; sz = sizeof(long); break; } case xsStringType: { char *s = xsToString(xsArg(0)); data = s; sz = strlen(s); break; } default: if (xsIsInstanceOf(xsArg(0), xsArrayBufferPrototype)) { data = xsToArrayBuffer(xsArg(0)); sz = xsGetArrayBufferLength(xsArg(0)); } else { data = NULL; sz = 0; } break; } mc_rng_init(data, sz); }
void xs_chacha_setIV(xsMachine *the) { crypt_stream_t *stream = xsGetHostData(xsThis); int ac = xsToInteger(xsArgc); void *iv; size_t ivsize; uint64_t counter = 0; if (ac > 0 && xsTest(xsArg(0))) { iv = xsToArrayBuffer(xsArg(0)); ivsize = xsGetArrayBufferLength(xsArg(0)); } else { iv = NULL; ivsize = 0; } if (ac > 1) { switch (xsTypeOf(xsArg(1))) { case xsIntegerType: case xsNumberType: counter = xsToInteger(xsArg(1)); /* @@ take only 32bit */ break; } } kcl_chacha_setIV(stream->ctx, iv, ivsize, counter); }
void xs_i2c_readWord(xsMachine *the) { wm_i2c *i2c = xsGetHostData(xsThis); int reg; int nbytes = xsToInteger(xsArg(1)); int lsbFirst = xsToInteger(xsArgc) > 2 ? xsToBoolean(xsArg(2)) : 0; uint8_t data[4]; switch (xsTypeOf(xsArg(0))) { case xsNumberType: case xsIntegerType: reg = xsToInteger(xsArg(0)); break; default: reg = -1; break; } if (nbytes > 4) nbytes = 4; if (mc_i2c_read(i2c, reg, data, nbytes) == 0) { uint32_t res = 0; int i; if (lsbFirst) { for (i = 0; i < nbytes; i++) res |= data[i] << (i * 8); } else { for (i = 0; i < nbytes; i++) res = (res << 8) | data[i]; } xsSetInteger(xsResult, res); } }
static void cipher_process(xsMachine *the, kcl_symmetric_direction_t direction) { crypt_cipher_t *cipher = xsGetHostData(xsThis); size_t len; void *indata, *outdata; if (cipher->keysched != NULL) { if (cipher->direction != direction) { (*cipher->keysched)(cipher->ctx, direction); cipher->direction = direction; } } if (xsToInteger(xsArgc) > 1 && xsTest(xsArg(1))) { if (xsGetArrayBufferLength(xsArg(1)) < (xsIntegerValue)cipher->blockSize) crypt_throw_error(the, "cipher: too small buffer"); xsResult = xsArg(1); } else xsResult = xsArrayBuffer(NULL, cipher->blockSize); if (xsTypeOf(xsArg(0)) == xsStringType) { indata = xsToString(xsArg(0)); len = c_strlen(indata); } else { indata = xsToArrayBuffer(xsArg(0)); len = xsGetArrayBufferLength(xsArg(0)); } if (len < cipher->blockSize) crypt_throw_error(the, "cipher: wrong size"); outdata = xsToArrayBuffer(xsResult); (*cipher->process)(cipher->ctx, indata, outdata); }
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); }
void xs_mod_init(xsMachine *the) { mod_t *mod; UInt32 options = xsToInteger(xsArgc) > 0 && xsTypeOf(xsArg(0)) == xsIntegerType ? xsToInteger(xsArg(0)): 0; FskErr ferr; if ((ferr = FskMemPtrNew(sizeof(mod_t), (FskMemPtr *)&mod)) != kFskErrNone) cryptThrowFSK(ferr); if ((ferr = FskMemPtrNew(sizeof(bn_mod_t), (FskMemPtr *)&mod->mod_data)) != kFskErrNone) { FskMemPtrDispose(mod); cryptThrowFSK(ferr); } mod->_mod_code = &modFuncs; xsResult = xsGet(xsThis, xsID("z")); /* must exist */ if (!xsIsInstanceOf(xsResult, xsGet(xsGet(xsGlobal, xsID("Arith")), xsID("z"))) || (mod->z = xsGetHostData(xsResult)) == NULL) cryptThrow("kCryptTypeError"); xsResult = xsGet(xsThis, xsID("m")); /* must exist */ if (!xsIsInstanceOf(xsResult, mod->z->z_protoInteger) || (mod->m = xsGetHostData(xsResult)) == NULL) nanError(); bn_mod_init(mod->mod_data, mod->z->z_data, mod->m->cint_data, options); xsSetHostData(xsThis, mod); }
void xs_chacha_init(xsMachine *the) { crypt_stream_t *stream = xsGetHostData(xsThis); int ac = xsToInteger(xsArgc); void *key, *iv; size_t keysize, ivsize; uint64_t counter = 0; kcl_err_t err; key = xsToArrayBuffer(xsArg(0)); keysize = xsGetArrayBufferLength(xsArg(0)); if (ac > 1 && xsTest(xsArg(1))) { iv = xsToArrayBuffer(xsArg(1)); ivsize = xsGetArrayBufferLength(xsArg(1)); } else { iv = NULL; ivsize = 0; } if (ac > 2) { switch (xsTypeOf(xsArg(2))) { case xsIntegerType: case xsNumberType: counter = xsToInteger(xsArg(2)); /* @@ take only 32bit */ break; } } if ((err = kcl_chacha_init(&stream->ctx, key, keysize, iv, ivsize, counter)) != KCL_ERR_NONE) kcl_throw_error(the, err); stream->process = kcl_chacha_process; stream->finish = kcl_chacha_finish; }
void KprDebugMachineLoadView(KprDebugMachine self, int theView, char* thePath, int theLine) { FskErr err = kFskErrNone; KprDebug debug = self->debug; xsMachine* the = debug->the; // FskFileInfo info; // FskFileMapping map = NULL; // FskDebugStr("%s: %d - %s:%d", __FUNCTION__, theView, thePath, theLine); if (xsTypeOf(debug->behavior) == xsUndefinedType) goto bail; xsVar(0) = xsAccess(debug->behavior); if (xsFindResult(xsVar(0), xsID("onMachineFileChanged"))) { (void)xsCallFunction5(xsResult, xsVar(0), xsString(self->address), xsInteger(theView), xsNull, thePath ? xsString(thePath) : xsNull, xsInteger(theLine)); } // if (kFskErrNone == FskFileGetFileInfo(thePath, &info) && (kFskDirectoryItemIsFile == info.filetype)) { // unsigned char *data; // FskInt64 size; // BAIL_IF_ERR(err = FskFileMap(thePath, &data, &size, 0, &map)); // xsVar(0) = xsAccess(debug->behavior); // // if (xsFindResult(xsVar(0), xsID("onMachineFileChanged"))) { // (void)xsCallFunction5(xsResult, xsVar(0), xsString(self->address), xsInteger(theView), xsNull, xsString(thePath), xsInteger(theLine)); // } // } bail: // FskFileDisposeMap(map); xsThrowIfFskErr(err); }
void KprDebugMachineEchoItem(KprDebugMachine self, int theView, char* path, int line, char* name, char* value) { KprDebug debug = self->debug; xsMachine* the = debug->the; if (xsTypeOf(debug->behavior) == xsUndefinedType) goto bail; { xsTry { xsVar(0) = xsAccess(debug->behavior); xsVar(1) = xsGet(xsVar(0), xsID(self->address)); xsVar(2) = xsNewInstanceOf(xsObjectPrototype); if (path) xsNewHostProperty(xsVar(2), xsID("path"), xsString(path), xsDefault, xsDontScript); if (line >= 0) xsNewHostProperty(xsVar(2), xsID("line"), xsInteger(line), xsDefault, xsDontScript); if (name) xsNewHostProperty(xsVar(2), xsID("name"), xsString(name), xsDefault, xsDontScript); if (value) xsNewHostProperty(xsVar(2), xsID("value"), xsString(value), xsDefault, xsDontScript); if (xsFindResult(xsVar(1), xsID_push)) { (void)xsCallFunction1(xsResult, xsVar(1), xsVar(2)); } } xsCatch { } } bail: return; }
void KPR_debug_get_behavior(xsMachine *the) { KprDebug self = xsGetHostData(xsThis); if (xsTypeOf(self->behavior) != xsUndefinedType) xsResult = self->behavior; else xsResult = xsUndefined; }
void Zeroconf_get_behavior(xsMachine *the) { KprZeroconfCommon self = xsGetHostData(xsThis); if (xsTypeOf(self->behavior) != xsUndefinedType) xsResult = self->behavior; else xsResult = xsUndefined; }
void KPR_debug_removeBreakpoint(xsMachine *the) { KprDebug self = xsGetHostData(xsThis); KprDebugMachine machine = NULL; xsIntegerValue c = xsToInteger(xsArgc); char* path = NULL; SInt32 line = -1; if ((c >= 3) && (xsTypeOf(xsArg(0)) == xsStringType) && (xsTypeOf(xsArg(1)) == xsStringType) && (xsTypeOf(xsArg(2)) == xsIntegerType)) { char* address = xsToString(xsArg(0)); path = xsToString(xsArg(1)); line = xsToInteger(xsArg(2)); machine = KprDebugFindMachine(self, address); } if (machine && path && (line >= 0)) KprDebugMachineDispatchCommand(machine, mxClearBreakpointCommand, path, NULL, line); else xsThrowIfFskErr(kFskErrInvalidParameter); }
void KPR_system_getenv(xsMachine *the) { xsIntegerValue c = xsToInteger(xsArgc); if ((c >= 1) && (xsTypeOf(xsArg(0)) == xsStringType)) { char* variable = xsToString(xsArg(0)); char* value = getenv(variable); if (value) xsResult = xsString(value); } }
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 KprMessageScriptTargetSet(KprMessage self, xsMachine* the, xsSlot* slot) { KprMessageScriptTarget stream = NULL; if (xsTypeOf(*slot) == xsStringType) { xsThrowIfFskErr(KprMessageScriptTargetNew(&stream, xsToString(*slot))); } else { stream = xsGetHostData(*slot); } KprMessageSetStream(self, (KprStream)stream); }
void kpr2jsMakePath(xsMachine* the) { char aPath[1024]; int aLength; char* aString; aPath[0] = 0; if (xsTypeOf(xsArg(0)) == xsStringType) { aString = xsToString(xsArg(0)); aLength = strlen(aString); if (aLength > 0) { strcat(aPath, aString); if (aPath[aLength - 1] != mxSeparator) { aPath[aLength] = mxSeparator; aPath[aLength + 1] = 0; } } } if (xsTypeOf(xsArg(1)) == xsStringType) { aString = xsToString(xsArg(1)); strcat(aPath, aString); } if (xsTypeOf(xsArg(2)) == xsStringType) { aString = xsToString(xsArg(2)); aLength = strlen(aString); if (aLength > 0) { if (aString[0] != '.') strcat(aPath, "."); strcat(aPath, aString); } } #if mxWindows aString = aPath; while (*aString) { if (*aString == '/') *aString = '\\'; aString++; } #endif xsResult = xsString(aPath); }
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 KPR_debug_trigger(xsMachine *the, UInt32 command) { KprDebug self = xsGetHostData(xsThis); KprDebugMachine machine = NULL; xsIntegerValue c = xsToInteger(xsArgc); if ((c >= 1) && (xsTypeOf(xsArg(0)) == xsStringType)) { char* address = xsToString(xsArg(0)); machine = KprDebugFindMachine(self, address); } if (machine && (command >= mxAbortCommand) && (command <= mxStepOutCommand)) KprDebugMachineDispatchCommand(machine, command, NULL, NULL, 0); else xsThrowIfFskErr(kFskErrInvalidParameter); }
void KPR_debug_toggle(xsMachine *the) { KprDebug self = xsGetHostData(xsThis); KprDebugMachine machine = NULL; int command = mxNoCommand; char* path = NULL; char* value = NULL; xsIntegerValue c = xsToInteger(xsArgc); if ((c >= 4) && (xsTypeOf(xsArg(0)) == xsStringType) && (xsTypeOf(xsArg(1)) == xsIntegerType) && (xsTypeOf(xsArg(3)) == xsStringType)) { char* address = xsToString(xsArg(0)); command = xsToInteger(xsArg(1)); if (xsTypeOf(xsArg(2)) == xsStringType) path = FskStrDoCopy(xsToString(xsArg(2))); value = FskStrDoCopy(xsToString(xsArg(3))); machine = KprDebugFindMachine(self, address); } if (machine && ((command == mxLocalsView) || (command == mxGlobalsView) || (command == mxGrammarsView)) && value) KprDebugMachineDispatchCommand(machine, command, path, value, 0); else xsThrowIfFskErr(kFskErrInvalidParameter); FskMemPtrDispose(value); FskMemPtrDispose(path); }
void KPR_debug_resetBreakpoints(xsMachine *the) { KprDebug self = xsGetHostData(xsThis); KprDebugMachine machine = NULL; xsIntegerValue c = xsToInteger(xsArgc); if ((c >= 1) && (xsTypeOf(xsArg(0)) == xsStringType)) { char* address = xsToString(xsArg(0)); machine = KprDebugFindMachine(self, address); } if (machine) KprDebugMachineDispatchCommand(machine, mxClearAllBreakpointsCommand, NULL, NULL, 0); else xsThrowIfFskErr(kFskErrInvalidParameter); }
void getInputData(xsMachine *the, xsSlot *input, void **datap, xsIntegerValue *sizep) { if (xsIsInstanceOf(*input, xsChunkPrototype)) { *datap = xsGetHostData(*input); *sizep = xsToInteger(xsGet(*input, xsID("length"))); } else if (xsTypeOf(*input) == xsStringType) { *datap = xsToString(*input); *sizep = FskStrLen(*datap); } else cryptThrow("kCryptTypeError"); }
void xs_i2c_init(xsMachine *the) { xsI2C i2c = NULL; FskErr err; int address; int sdaPin = 0, clkPin = 0, sdaDev = 0, clkDev = 0; FskPinI2C pin = NULL; if (xsGetHostData(xsThis)) xsThrowDiagnosticIfFskErr(kFskErrBadState, "I2C pin already initialized (SDA pin %d).", i2c->sdaPin); address = xsToInteger(xsGet(xsThis, xsID("address"))); xsResult = xsGet(xsThis, xsID("bus")); if (xsUndefinedType == xsTypeOf(xsResult)) { sdaPin = xsToInteger(xsGet(xsThis, xsID("sda"))); clkPin = xsToInteger(xsGet(xsThis, xsID("clock"))); err = FskPinI2CNew(&pin, sdaPin, clkPin, kFskPinI2CNoBus); } else { sdaDev = xsToInteger(xsResult); err = FskPinI2CNew(&pin, 0, 0, sdaDev); } xsThrowDiagnosticIfFskErr(err, "I2C open failed %s (SDA pin %d, CLK pin %d).", FskInstrumentationGetErrorString(err), sdaPin, clkPin); err = FskMemPtrNewClear(sizeof(xsI2CRecord), &i2c); if (err) { FskPinI2CDispose(pin); xsError(err); } xsSetHostData(xsThis, i2c); i2c->pin = pin; i2c->sdaPin = sdaPin; i2c->clkPin = clkPin; i2c->sdaDev = sdaDev; i2c->clkDev = clkDev; i2c->address = (UInt8)address; i2c->bus = sdaDev; #if SUPPORT_XS_DEBUG if (0 != sdaPin) snprintf(i2c->diagnosticID, sizeof(i2c->diagnosticID), "(Address 0x%x, SDA pin %d, CLK pin %d)", address, sdaPin, clkPin); else snprintf(i2c->diagnosticID, sizeof(i2c->diagnosticID), "(Address 0x%x, Bus %d)", address, sdaDev); #endif }
void Zeroconf_Advertisement(xsMachine *the) { KprZeroconfAdvertisement self = NULL; char* serviceType = xsToString(xsArg(0)); char* servicName = xsToString(xsArg(1)); int servicPort = xsToInteger(xsArg(2)); xsIntegerValue c = xsToInteger(xsArgc); FskAssociativeArray txt = NULL; if ((c > 3) && xsIsInstanceOf(xsArg(3), xsObjectPrototype)) { xsVars(2); xsEnterSandbox(); fxPush(xsArg(3)); fxRunForIn(the); txt = FskAssociativeArrayNew(); for (xsVar(0) = fxPop(); xsTypeOf(xsVar(0)) != xsNullType; xsVar(0) = fxPop()) { if (xsTypeOf(xsVar(0)) == xsStringType) { xsVar(1) = xsGetAt(xsArg(3), xsVar(0)); if (!xsIsInstanceOf(xsVar(1), xsObjectPrototype)) { char* name = xsToString(xsVar(0)); char* value = xsToString(xsVar(1)); FskAssociativeArrayElementSetString(txt, name, value); } } } xsLeaveSandbox(); } xsThrowIfFskErr(KprZeroconfAdvertisementNew(&self, serviceType, servicName, servicPort, txt)); xsSetHostData(xsResult, self); self->registeredCallback = Zeroconf_advertisement_registeredCallback; self->unregisteredCallback = Zeroconf_advertisement_unregisteredCallback; self->the = the; self->slot = xsResult; self->code = the->code; self->behavior = xsUndefined; xsRemember(self->slot); }
void xsToolSearchProperty(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) xsResult = xsCall1(xsResult, xsID("searchProperty"), xsArg(0)); }
static void KPR_canvasRenderingContext2D_setStyle(xsMachine *the, xsBooleanValue stroke) { if (xsTypeOf(xsArg(0)) == xsStringType) { xsStringValue s = xsToString(xsArg(0)); FskColorRGBARecord color; if (KprParseColor(the, s, &color)) { FskCanvas2dContext ctx = xsGetHostData(xsThis); if (stroke) FskCanvas2dSetStrokeStyleColor(ctx, &color); else FskCanvas2dSetFillStyleColor(ctx, &color); } } else xsArg(0) = xsCall2(xsArg(0), xsID("setStyle"), xsThis, xsBoolean(stroke)); }
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)); }