void GLWidget::mouseReleaseEvent ( QMouseEvent * qevent ) { caValue* event = circa_alloc_list(2); circa_set_int(circa_index(event, 0), 2); circa_set_vec2(circa_index(event, 1), qevent->x(), qevent->y()); onInputEvent(event); }
void tv_static_type_query(Type* type, StaticTypeQuery* query) { Term* subject = query->subject; Type* subjectType = query->subjectType; // If the type doesn't have a specific size, then accept any list. if (!list_type_has_specific_size(&type->parameter)) { if (is_list_based_type(subjectType)) return query->succeed(); else return query->fail(); } caValue* expectedElementTypes = list_get_type_list_from_type(type); // Special case when looking at a call to list(); look at inputs instead of // looking at the result. if (subject && subject->function == FUNCS.list) { if (subject->numInputs() != circa_count(expectedElementTypes)) return query->fail(); for (int i=0; i < circa_count(expectedElementTypes); i++) if (!circa::term_output_always_satisfies_type( subject->input(i), as_type(circa_index(expectedElementTypes, i)))) return query->fail(); return query->succeed(); } // Look at the subject's type. if (!list_type_has_specific_size(&subjectType->parameter)) return query->fail(); caValue* subjectElementTypes = list_get_type_list_from_type(subjectType); bool anyUnableToDetermine = false; for (int i=0; i < circa_count(expectedElementTypes); i++) { if (i >= circa_count(subjectElementTypes)) return query->fail(); StaticTypeQuery::Result result = run_static_type_query( as_type(circa_index(expectedElementTypes,i)), as_type(circa_index(subjectElementTypes,i))); // If any of these fail, then fail. if (result == StaticTypeQuery::FAIL) return query->fail(); if (result == StaticTypeQuery::UNABLE_TO_DETERMINE) anyUnableToDetermine = true; } if (anyUnableToDetermine) return query->unableToDetermine(); else return query->succeed(); }
void Branch__get_static_errors_formatted(caStack* stack) { Branch* branch = as_branch(circa_input(stack, 0)); if (branch == NULL) return circa_output_error(stack, "NULL branch"); if (is_null(&branch->staticErrors)) set_list(circa_output(stack, 0), 0); caValue* errors = &branch->staticErrors; caValue* out = circa_output(stack, 0); set_list(out, circa_count(errors)); for (int i=0; i < circa_count(out); i++) format_static_error(circa_index(errors, i), circa_index(out, i)); }
void make_client(Stack* stack) { printf("make_client\n"); Connection* connection = new Connection(); ca_assert(stack->world->libuvWorld->uv_loop != NULL); uv_tcp_init(stack->world->libuvWorld->uv_loop, &connection->uv_tcp); connection->uv_tcp.data = connection; Value* ip = circa_input(stack, 0); Value* port = circa_input(stack, 1); sockaddr_in bind_addr = uv_ip4_addr(circa_string(ip), circa_int(port)); #if 0 if (uv_ip4_addr(circa_string(ip), circa_int(port), &bind_addr)) { printf("error from uv_ip4_addr\n"); return; } #endif uv_connect_t* uv_connect = (uv_connect_t*) malloc(sizeof(*uv_connect)); uv_connect->data = connection; if (uv_tcp_connect(uv_connect, &connection->uv_tcp, bind_addr, client_on_connect)) { printf("uv_tcp_connect error\n"); } Value* out = circa_set_default_output(stack, 0); circa_set_native_ptr(circa_index(out, 0), connection, ConnectionRelease); printf("make_client fin\n"); }
void dynamic_method_call(caStack* stack) { INCREMENT_STAT(DynamicMethodCall); caValue* args = circa_input(stack, 0); caValue* object = circa_index(args, 0); // Lookup method Term* term = (Term*) circa_caller_term(stack); std::string functionName = term->stringProp("syntax:functionName", ""); // Find and dispatch method Term* method = find_method((Block*) circa_caller_block(stack), (Type*) circa_type_of(object), functionName.c_str()); // copy(object, circa_output(stack, 1)); if (method != NULL) { // Grab inputs before pop Value inputs; swap(args, &inputs); pop_frame(stack); push_frame_with_inputs(stack, function_contents(method), &inputs); return; } // Method not found. Raise error. std::string msg; msg += "Method "; msg += functionName; msg += " not found on type "; msg += as_cstring(&circa_type_of(object)->name); circa_output_error(stack, msg.c_str()); }
void load_font(caStack* stack) { caValue* filename = circa_input(stack, 0); FontFace* font = new FontFace(); circa_read_file_with_stack(stack, circa_string(filename), &font->rawData); if (circa_is_null(&font->rawData)) return circa_output_error(stack, "Failed to load file"); if (!g_ftLibraryInitialized) { FT_Init_FreeType(&g_ftLibrary); g_ftLibraryInitialized = true; } int error = FT_New_Memory_Face(g_ftLibrary, (FT_Byte*) circa_blob(&font->rawData), circa_blob_size(&font->rawData), 0, &font->ftFace); if (error) return circa_output_error(stack, "FT_New_Memory_Face failed"); font->cairoFace = cairo_ft_font_face_create_for_ft_face(font->ftFace, 0); caValue* out = circa_set_default_output(stack, 0); circa_set_native_ptr(circa_index(out, 0), font, FontFaceRelease); }
void GLWidget::onInputEvent(caValue* event) { scripts_pre_message_send(); caValue* msg = circa_alloc_list(2); circa_set_string(circa_index(msg, 0), "onInputEvent"); circa_move(event, circa_index(msg, 1)); circa_dealloc_value(event); circa_actor_run_message(g_world, "View", msg); circa_dealloc_value(msg); scripts_post_message_send(); }
void Connection__send(Stack* stack) { Connection* connection = (Connection*) circa_native_ptr(circa_index(circa_input(stack, 0), 0)); Value* asStr = circa_alloc_value(); circa_to_string(circa_input(stack, 1), asStr); circa_uv_write((uv_stream_t*) &connection->uv_tcp, asStr, true); }
void Block__get_static_errors_formatted(VM* vm) { Block* block = as_block(vm->input(0)); if (block == NULL) return vm->throw_str("NULL block"); Value* errors = block_get_static_errors(block); if (errors == NULL) { set_list(vm->output(), 0); return; } Value* out = vm->output(); set_list(out, circa_count(errors)); for (int i=0; i < circa_count(out); i++) format_static_error(circa_index(errors, i), circa_index(out, i)); }
void Term__inputs(caStack* stack) { Term* t = as_term_ref(circa_input(stack, 0)); if (t == NULL) return circa_output_error(stack, "NULL reference"); caValue* output = circa_output(stack, 0); circa_set_list(output, t->numInputs()); for (int i=0; i < t->numInputs(); i++) set_term_ref(circa_index(output, i), t->input(i)); }
void Branch__terms(caStack* stack) { Branch* branch = as_branch(circa_input(stack, 0)); if (branch == NULL) return circa_output_error(stack, "NULL branch"); caValue* out = circa_output(stack, 0); set_list(out, branch->length()); for (int i=0; i < branch->length(); i++) set_term_ref(circa_index(out, i), branch->get(i)); }
void Block__terms(VM* vm) { Block* block = as_block(vm->input(0)); if (block == NULL) return vm->throw_str("NULL block"); Value* out = vm->output(); set_list(out, block->length()); for (int i=0; i < block->length(); i++) set_term_ref(circa_index(out, i), block->get(i)); }
void make_server(Stack* stack) { Value* ip = circa_input(stack, 0); Value* port = circa_input(stack, 1); Value* type = circa_input(stack, 2); Server* server = new Server(); if (circa_string_equals(type, ":tcp")) { server->serverType = TCP; } else if (circa_string_equals(type, ":websock")) { server->serverType = WEBSOCK; memset(&server->parser_settings, 0, sizeof(server->parser_settings)); server->parser_settings.on_message_begin = http_on_message_begin; server->parser_settings.on_url = http_on_url; server->parser_settings.on_status = http_on_status; server->parser_settings.on_header_field = http_on_header_field; server->parser_settings.on_header_value = http_on_header_value; server->parser_settings.on_headers_complete = http_on_headers_complete; server->parser_settings.on_body = http_on_body; server->parser_settings.on_message_complete = http_on_message_complete; } else { Value msg; circa_set_string(&msg, "Unrecognized server type: "); circa_string_append_val(&msg, type); circa_output_error_val(stack, &msg); delete server; return; } uv_loop_t* loop = get_uv_loop(stack->world); circa_set_list(&server->connections, 0); uv_tcp_init(stack->world->libuvWorld->uv_loop, &server->uv_tcp); struct sockaddr_in bind_addr = uv_ip4_addr(circa_string(ip), circa_int(port)); uv_tcp_bind(&server->uv_tcp, bind_addr); int err = uv_listen((uv_stream_t*) &server->uv_tcp, 128, server_on_connect); server->uv_tcp.data = server; if (err) { Value msg; circa_set_string(&msg, "Listen error: "); circa_string_append(&msg, uv_err_name(uv_last_error(loop))); circa_output_error_val(stack, &msg); return; } Value* out = circa_set_default_output(stack, 0); circa_set_native_ptr(circa_index(out, 0), server, ServerRelease); }
void Term__inputs(VM* vm) { Term* t = as_term_ref(vm->input(0)); if (t == NULL) return vm->throw_str("NULL term"); Value* output = vm->output(); circa_set_list(output, t->numInputs()); for (int i=0; i < t->numInputs(); i++) set_term_ref(circa_index(output, i), t->input(i)); }
void GLWidget::paintEvent(QPaintEvent*) { QPainter painter; painter.begin(this); painter.setRenderHint(QPainter::Antialiasing); scripts_pre_message_send(); // call onPaintEvent caValue* msg = circa_alloc_list(2); circa_set_string(circa_index(msg, 0), "onPaintEvent"); circa_set_typed_pointer(circa_index(msg, 1), circa_find_type(NULL, "Painter"), &painter); circa_actor_run_message(g_world, "View", msg); circa_dealloc_value(msg); scripts_post_message_send(); painter.end(); }
int list_find_field_index_by_name(Type* listType, const char* name) { if (!is_list_based_type(listType)) return -1; caValue* names = list_get_name_list_from_type(listType); if (names == NULL) return -1; for (int i=0; i < circa_count(names); i++) if (string_eq(circa_index(names, i), name)) return i; // Not found return -1; }
void average(caStack* stack) { caValue* args = circa_input(stack, 0); int count = circa_count(args); caValue* out = circa_output(stack, 0); if (count == 0) { set_float(out, 0); return; } float sum = 0; for (int i=0; i < count; i++) sum += to_float(circa_index(args, i)); set_float(out, sum / count); }
void test_cast_first_inputs() { // Pass an input of [1] to a branch that expects a compound type. // The function will need to cast the [1] to T in order for it to work. Branch branch; branch.compile("type T { int i }"); Term* f = branch.compile("def f(T t) -> int { return t.i }"); Stack context; push_frame(&context, function_contents(f)); caValue* in = circa_input((caStack*) &context, 0); circa_set_list(in, 1); circa_set_int(circa_index(in, 0), 5); run_interpreter(&context); test_assert(circa_int(circa_output((caStack*) &context, 0)) == 5); }
// Pack a state value. Each input will correspond with a slot in the branch's state type. void pack_state(caStack* stack) { Term* caller = (Term*) circa_caller_term(stack); Branch* branch = caller->owningBranch; if (branch->stateType == NULL) return; caValue* args = circa_input(stack, 0); caValue* output = circa_output(stack, 0); create(branch->stateType, output); for (int i=0; i < circa_count(args); i++) { caValue* input = circa_index(args, i); caValue* dest = list_get(output, i); if (input == NULL) set_null(dest); else copy(input, dest); } }
static void server_on_connect(uv_stream_t *uv_server, int status) { Server* server = (Server*) uv_server->data; if (status == -1) { printf("server_on_connect failed\n"); return; } Connection* connection = new Connection(); connection->server = server; uv_tcp_init(uv_server->loop, &connection->uv_tcp); connection->uv_tcp.data = connection; switch (connection->server->serverType) { case WEBSOCK: http_parser_init(&connection->parser, HTTP_REQUEST); circa_set_map(&connection->httpHeaders); connection->parser.data = connection; connection->state = WEBSOCK_NEGOTIATE_STATE; break; case TCP: connection->state = TCP_STATE; break; } if (uv_accept(uv_server, (uv_stream_t*) &connection->uv_tcp) != 0) internal_error("uv_accept error\n"); if (uv_read_start((uv_stream_t*) &connection->uv_tcp, alloc_buffer, on_read) != 0) internal_error("uv_read_start error\n"); Value* wrapped = circa_append(&server->connections); circa_set_list(wrapped, 1); circa_set_native_ptr(circa_index(wrapped, 0), connection, ConnectionRelease); printf("on new connection\n"); }
void Connection__receive(Stack* stack) { Connection* connection = (Connection*) circa_native_ptr(circa_index(circa_input(stack, 0), 0)); circa_move(&connection->incomingMsgs, circa_output(stack, 0)); circa_set_list(&connection->incomingMsgs, 0); }
FontFace* as_font_face(caValue* value) { return (FontFace*) circa_native_ptr(circa_index(value, 0)); }
void ImprovWindow::mainLoop() { // Main loop. while (true) { // Handle events. SDL_Event sdlEvent; while (SDL_PollEvent(&sdlEvent)) { switch (sdlEvent.type) { case SDL_QUIT: printf("received SDL_QUIT\n"); return; case SDL_KEYDOWN: { #ifndef NACL // Quit on Escape key if (sdlEvent.key.keysym.sym == SDLK_ESCAPE) return; #if 0 // Fullscreen on command-F if (sdlEvent.key.keysym.sym == SDLK_f && (sdlEvent.key.keysym.mod & KMOD_CTRL)) toggleFullscreen(); #endif #endif caValue* event = circa_append(_inputEvents); circa_set_list(event, 2); circa_set_symbol(circa_index(event, 0), "down"); sdl_key_to_symbol(sdlEvent.key.keysym.sym, circa_index(event, 1)); break; } case SDL_KEYUP: { caValue* event = circa_append(_inputEvents); circa_set_list(event, 2); circa_set_symbol(circa_index(event, 0), "up"); sdl_key_to_symbol(sdlEvent.key.keysym.sym, circa_index(event, 1)); break; } case SDL_MOUSEBUTTONDOWN: { double mouseX = sdlEvent.button.x; double mouseY = sdlEvent.button.y; caValue* event = circa_append(_inputEvents); circa_set_list(event, 3); circa_set_symbol(circa_index(event, 0), "down"); if (sdlEvent.button.button == SDL_BUTTON_LEFT) circa_set_symbol(circa_index(event, 1), "left_mouse"); else circa_set_symbol(circa_index(event, 1), "right_mouse"); circa_set_vec2(circa_index(event, 2), mouseX, mouseY); break; } case SDL_MOUSEBUTTONUP: { double mouseX = sdlEvent.button.x; double mouseY = sdlEvent.button.y; caValue* event = circa_append(_inputEvents); circa_set_list(event, 3); circa_set_symbol(circa_index(event, 0), "up"); if (sdlEvent.button.button == SDL_BUTTON_LEFT) circa_set_symbol(circa_index(event, 1), "left_mouse"); else circa_set_symbol(circa_index(event, 1), "right_mouse"); circa_set_vec2(circa_index(event, 2), mouseX, mouseY); break; } case SDL_MOUSEWHEEL: { caValue* event = circa_append(_inputEvents); circa_set_list(event, 3); circa_set_symbol(circa_index(event, 0), "mouse_wheel"); circa_set_vec2(circa_index(event, 1), sdlEvent.wheel.x, sdlEvent.wheel.y); break; } case SDL_WINDOWEVENT: { switch (sdlEvent.window.event) { case SDL_WINDOWEVENT_RESIZED: int width = sdlEvent.window.data1; int height = sdlEvent.window.data2; setSize(width, height); break; } break; } } } // Done handling events. Redraw screen. _elapsedTime = SDL_GetTicks() / 1000.0; int mouseX, mouseY; SDL_GetMouseState(&mouseX, &mouseY); _mouseX = mouseX; _mouseY = mouseY; redraw(); SDL_GL_SwapWindow(_sdl_window); SDL_Delay(1); } }
void Server__connections(Stack* stack) { Server* server = (Server*) circa_native_ptr(circa_index(circa_input(stack, 0), 0)); circa_copy(&server->connections, circa_output(stack, 0)); }