Ejemplo n.º 1
0
void KprMessageScriptTargetGet(KprMessage self, xsMachine* the, xsSlot* slot)
{
	if (self->stream->dispatch->dispose == KprMessageScriptTargetDispose) {
		KprMessageScriptTarget target = (KprMessageScriptTarget)self->stream;
		if (target->result)
			*slot = xsDemarshall(target->result);
		else {
			void* data;
			UInt32 size; 
			KprMessageGetResponseBody(self, &data, &size);
			if (data && size) {
				if (!FskStrCompare(target->name, "TEXT"))
					*slot = xsStringBuffer(data, size);
				else {
					*slot = xsNewInstanceOf(xsChunkPrototype);
					xsSetHostData(*slot, data);
					xsSetHostDestructor(*slot , NULL);
					xsSet(*slot, xsID("length"), xsInteger(size));		//@@jph
				}
			}
		}
	}
}
Ejemplo n.º 2
0
void
xs_z_init(xsMachine *the)
{
	z_t *z;
	kcl_err_t err;
	static kcl_error_callback_t cb;

	if ((z = crypt_malloc(sizeof(z_t))) == NULL)
		kcl_throw_error(the, KCL_ERR_NOMEM);
	if ((err = kcl_z_alloc(&z->ctx)) != KCL_ERR_NONE) {
		crypt_free(z);
		kcl_throw_error(the, err);
	}
	z->proto_int = xsGet(xsThis, xsID("_proto_int"));
	cb.f = kcl_z_error_callback;
	cb.closure = the;
	if ((err = kcl_z_init(z->ctx, &cb)) != KCL_ERR_NONE) {
		kcl_z_dispose(z->ctx);
		crypt_free(z);
		kcl_throw_error(the, err);
	}
	xsSetHostData(xsThis, z);
}
Ejemplo n.º 3
0
void KPR_MQTTClient(xsMachine* the)
{
	FskErr err;
	KPR_MQTTClientRecord *self = NULL;
	KprMQTTClient client = NULL;
	char *clientIdentifier;
	Boolean cleanSession;

	if (xsToInteger(xsArgc) != 2) xsThrowIfFskErr(kFskErrParameterError);

	clientIdentifier = xsToString(xsArg(0));
	cleanSession = xsToBoolean(xsArg(1));

	bailIfError(FskMemPtrNewClear(sizeof(KPR_MQTTClientRecord), &self));
	bailIfError(KprMQTTClientNew(&client, clientIdentifier, cleanSession, kKprMQTTProtocol311, self));

	client->connectCallback = KPR_mqttclient_onConnect;
	client->subscribeCallback = KPR_mqttclient_onSubscribe;
	client->unsubscribeCallback = KPR_mqttclient_onUnsubscribe;
	client->publishCallback = KPR_mqttclient_onPublish;
	client->messageCallback = KPR_mqttclient_onMessage;
	client->disconnectCallback = KPR_mqttclient_onDisconnect;
	client->errorCallback = KPR_mqttclient_onError;

	self->client = client;
	self->the = the;
	self->slot = xsThis;
	xsSetHostData(self->slot, self);

bail:

	if (err) {
		KprMQTTClientDispose(client);
		FskMemPtrDispose(self);
		xsThrowIfFskErr(err);
	}
}
Ejemplo n.º 4
0
void KPR_WebSocketClient(xsMachine* the)
{
	FskErr err;
	xsIntegerValue c = xsToInteger(xsArgc);
	KPR_WebSocketClientRecord *self = NULL;
	KprWebSocketEndpoint endpoint = NULL;
	
	bailIfError(FskMemPtrNewClear(sizeof(KPR_WebSocketClientRecord), &self));
	
	xsThrowIfFskErr(KprWebSocketEndpointNew(&endpoint, self));
	
	endpoint->openCallback = KPR_WebSocketClient_onOpen;
	endpoint->closeCallback = KPR_WebSocketClient_onClose;
	endpoint->textCallback = KPR_WebSocketClient_onTextMessage;
	endpoint->binaryCallback = KPR_WebSocketClient_onBinaryMessage;
	endpoint->errorCallback = KPR_WebSocketClient_onError;
	
	self->endpoint = endpoint;
	self->the = the;
	self->slot = xsThis;
	self->code = the->code;
	xsSetHostData(self->slot, self);
	// xsCall1(xsGet(xsGlobal, xsID_Object), xsID_seal, self->slot);
	
	if (c >= 1) {
		bailIfError(KprWebSocketEndpointConnect(endpoint, xsToString(xsArg(0)), NULL));
	}

	xsRemember(self->slot);

	return;
	
bail:
	KprWebSocketEndpointDispose(endpoint);
	FskMemPtrDispose(self);
	xsThrowIfFskErr(err);
}
Ejemplo n.º 5
0
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);
}
Ejemplo n.º 6
0
void xs_pwm_init(xsMachine* the)
{
    FskErr err;
    FskPinPWM pwm;
    SInt32 pin = 0;
    char *pinName = NULL;

    xsVars(1);

    pwm = xsGetHostData(xsThis);
    if (pwm)
        xsThrowDiagnosticIfFskErr(kFskErrBadState, "PWM pin %d already initialized.", (int)pin);

    xsVar(0) = xsGet(xsThis, xsID("pin"));
    if (xsStringType == xsTypeOf(xsVar(0)))
        pinName = xsToString(xsVar(0));
    else
        pin = xsToInteger(xsVar(0));

	err = FskPinPWMNew(&pwm, pin, pinName);
    xsThrowDiagnosticIfFskErr(err, "PWM initialization of pin %d failed with error %d.", (int)pin, FskInstrumentationGetErrorString(err));

    xsSetHostData(xsThis, pwm);
}
Ejemplo n.º 7
0
void
xs_env_constructor(xsMachine *the)
{
	int ac = xsToInteger(xsArgc);
	const char *partname = ac > 0 ? xsToString(xsArg(0)) : MC_ENV_DEFAULT_PATH;
	int autosave = ac > 1 && xsTest(xsArg(1));
	int encrypt = ac > 2 && xsTest(xsArg(2));
	int recovery = ac <= 3 || xsTest(xsArg(3));
	mc_env_t *env;

	if (mc_env_init())
		mc_xs_throw(the, "mc_env: init fail");

	if (strcmp(partname, MC_ENV_DEFAULT_PATH) == 0)
		env = NULL;
	else {
		if ((env = mc_malloc(sizeof(mc_env_t))) == NULL)
			mc_xs_throw(the, "mc_env: no mem");
		if (mc_env_new(env, partname, encrypt, recovery) != 0)
			mc_xs_throw(the, "mc_env: new fail");
	}
	mc_env_autosave(env, autosave);
	xsSetHostData(xsThis, env);
}
Ejemplo n.º 8
0
static void
__arith_set_ecp(xsMachine *the, xsSlot *slot, kcl_ecp_t *p, xsSlot *proto)
{
	*slot = xsNewInstanceOf(*proto);
	xsSetHostData(*slot, p);
}
Ejemplo n.º 9
0
void kpr2jsOpenFile(xsMachine* the)
{
    FILE* aFile = fopen(xsToString(xsArg(0)), "w");
    xsElseError(aFile);
    xsSetHostData(xsThis, aFile);
}
Ejemplo n.º 10
0
void kpr2jsCloseFile(xsMachine* the)
{
    FILE* aFile = xsGetHostData(xsThis);
    fclose(aFile);
    xsSetHostData(xsThis, NULL);
}
Ejemplo n.º 11
0
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);
	}
}
Ejemplo n.º 12
0
static xsSlot _xsHostData(xsMachine *the, void *ptr)
{
	the->scratch = xsNewHostObject(NULL);
	xsSetHostData(the->scratch, ptr);
	return the->scratch;
}
Ejemplo n.º 13
0
void xs_pwm_close(xsMachine* the)
{
    xs_pwm(xsGetHostData(xsThis));
    xsSetHostData(xsThis, NULL);
}
Ejemplo n.º 14
0
void
xs_tftpd_close(xsMachine *the)
{
	tftpd_close(xsGetHostData(xsThis));
	xsSetHostData(xsThis, NULL);
}