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 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 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_host_debugger(xsMachine* the) { KprHost self = xsGetHostData(xsThis); KprApplication application = (KprApplication)self->first; xsBeginHostSandboxCode(application->the, NULL); { xsDebugger(); } xsEndHostSandboxCode(); }
void KPR_host_purge(xsMachine* the) { KprHost self = xsGetHostData(xsThis); KprApplication application = (KprApplication)self->first; xsBeginHostSandboxCode(application->the, NULL); { FskInstrumentedItemSendMessageNormal(self, kprInstrumentedContentBeginCollect, self); xsCollectGarbage(); FskInstrumentedItemSendMessageNormal(self, kprInstrumentedContentEndCollect, self); } xsEndHostSandboxCode(); KprContextPurge(application, false); }
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 KprDebugMachineDispose(KprDebugMachine self) { if (self) { KprDebug debug = self->debug; xsBeginHostSandboxCode(debug->the, debug->code); xsVars(1); KprDebugMachineCallbackText(self, "onMachineUnregistered", mxNoCommand, NULL); xsEndHostSandboxCode(); FskListRemove(&self->debug->machine, self); KprSocketWriterDispose(self->writer); self->writer = NULL; FskThreadRemoveDataHandler(&self->reader); FskNetSocketClose(self->socket); self->socket = NULL; FskMemPtrDisposeAt(&self->title); FskInstrumentedItemDispose(self); FskMemPtrDispose(self); } }
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(); }
static void KPRLibrarySniffPodcastComplete(KprMessage message, void* it) { KprFunctionTarget self = it; char* mime = KprMessageGetResponseHeader(message, "content-type"); char* sniff = NULL; if (message->response.body) { if (kFskErrNone == FskMediaPlayerSniffForMIME(message->response.body, message->response.size, NULL, message->url, &sniff)) { mime = sniff; } } { xsBeginHostSandboxCode(self->the, self->code); if (mime) xsResult = xsString(mime); else xsResult = xsString(""); (void)xsCallFunction1(self->slot, xsGlobal, xsResult); xsEndHostSandboxCode(); } FskMemPtrDispose(sniff); }
void KPR_host_quitting(xsMachine* the) { KprContainer container = xsGetHostData(xsThis); KprContent content = container->first; KprScriptBehavior self = (KprScriptBehavior)content->behavior; #if SUPPORT_INSTRUMENTATION void* params[2] = { self, "xsID_onQuitting" }; FskInstrumentedItemSendMessageDebug(content, kprInstrumentedContentCallBehavior, params); #endif if (self) { xsBeginHostSandboxCode(self->the, self->code); { xsVars(2); xsVar(0) = xsAccess(self->slot); if (xsFindResult(xsVar(0), xsID_onQuitting)) { xsVar(1) = kprContentGetter(content); (void)xsCallFunction1(xsResult, xsVar(0), xsVar(1)); } } xsEndHostSandboxCode(); } }
void KprFILEServiceInvoke(KprService service UNUSED, KprMessage message) { FskErr err = kFskErrNone; char* path = NULL; FskFileInfo info; FskDirectoryIterator iterator = NULL; char* pathName = NULL; char* sniff = NULL; FskFile fref = NULL; char *name = NULL; if (KprMessageContinue(message)) { if (!message->method || FskStrCompare(message->method, "GET")) { bailIfError(KprURLToPath(message->url, &path)); bailIfError(FskFileGetFileInfo(path, &info)); if (kFskDirectoryItemIsDirectory == info.filetype) { unsigned char buffer[4096]; UInt32 itemType; double date; UInt32 size; xsBeginHostSandboxCode(gFILEService.machine, NULL); { xsVars(3); { xsTry { xsVar(0) = xsGet(xsGlobal, xsID("Files")); xsResult = xsNewInstanceOf(xsArrayPrototype); bailIfError(FskDirectoryIteratorNew(path, &iterator, 0)); while (kFskErrNone == FskDirectoryIteratorGetNext(iterator, &name, &itemType)) { if (name[0] == '.') { FskMemPtrDisposeAt(&name); continue; } if (kFskDirectoryItemIsFile == itemType) { pathName = FskStrDoCat(path, name); bailIfError(FskFileGetFileInfo(pathName, &info)); bailIfError(FskFileOpen(pathName, kFskFilePermissionReadOnly, &fref)); bailIfError(FskFileRead(fref, sizeof(buffer), buffer, &size)); FskFileClose(fref); fref = NULL; if (kFskErrNone == FskMediaPlayerSniffForMIME(buffer, size, NULL, pathName, &sniff)) { } else if (kFskErrNone == FskImageDecompressSniffForMIME(buffer, size, NULL, pathName, &sniff)) { } FskMemPtrDispose(pathName); pathName = NULL; } xsVar(1) = xsNewInstanceOf(xsObjectPrototype); xsNewHostProperty(xsVar(1), xsID("name"), xsString(name), xsDefault, xsDontScript); xsNewHostProperty(xsVar(1), xsID("path"), xsString(name), xsDefault, xsDontScript); FskStrCopy((char*)buffer, message->url); FskStrCat((char*)buffer, name); if (kFskDirectoryItemIsDirectory == itemType) { xsVar(2) = xsGet(xsVar(0), xsID("directoryType")); FskStrCat((char*)buffer, "/"); } else if (kFskDirectoryItemIsFile == itemType) { xsVar(2) = xsGet(xsVar(0), xsID("fileType")); if (info.fileModificationDate > info.fileCreationDate) date = info.fileModificationDate * 1000.0; else date = info.fileCreationDate * 1000.0; xsNewHostProperty(xsVar(1), xsID("date"), xsNumber(date), xsDefault, xsDontScript); xsNewHostProperty(xsVar(1), xsID("size"), xsNumber(info.filesize), xsDefault, xsDontScript); if (sniff) { xsNewHostProperty(xsVar(1), xsID("mime"), xsString(sniff), xsDefault, xsDontScript); FskMemPtrDispose(sniff); sniff = NULL; } } else xsVar(2) = xsGet(xsVar(0), xsID("linkType")); xsNewHostProperty(xsVar(1), xsID("type"), xsVar(2), xsDefault, xsDontScript); xsNewHostProperty(xsVar(1), xsID("url"), xsString((char*)buffer), xsDefault, xsDontScript); (void)xsCall1(xsResult, xsID("push"), xsVar(1)); FskMemPtrDisposeAt(&name); } xsResult = xsCall1(xsGet(xsGlobal, xsID("JSON")), xsID("stringify"), xsResult); message->response.body = FskStrDoCopy(xsToString(xsResult)); message->response.size = FskStrLen(message->response.body); KprMessageTransform(message, gFILEService.machine); } xsCatch { } } } xsEndHostSandboxCode(); } else if (kFskDirectoryItemIsFile == info.filetype) {