void sl_init_ext_mysql(sl_vm_t* vm) { SLVAL MySQL, MySQL_Error, MySQL_Statement; MySQL = sl_define_class(vm, "MySQL", vm->lib.Object); sl_class_set_allocator(vm, MySQL, allocate_mysql); sl_define_method(vm, MySQL, "init", 3, sl_mysql_init); sl_define_method(vm, MySQL, "use", 1, sl_mysql_use); sl_define_method(vm, MySQL, "query", -2, sl_mysql_query); sl_define_method(vm, MySQL, "prepare", 1, sl_mysql_prepare); sl_define_method(vm, MySQL, "escape", 1, sl_mysql_escape); sl_define_method(vm, MySQL, "insert_id", 0, sl_mysql_insert_id); MySQL_Error = sl_define_class3(vm, sl_intern(vm, "Error"), vm->lib.Error, MySQL); MySQL_Statement = sl_define_class3(vm, sl_intern(vm, "Statement"), vm->lib.Object, MySQL); sl_class_set_allocator(vm, MySQL_Statement, allocate_mysql_stmt); sl_define_method(vm, MySQL_Statement, "param_count", 0, sl_mysql_stmt_param_count); sl_define_method(vm, MySQL_Statement, "execute", -1, sl_mysql_stmt_execute); sl_define_method(vm, MySQL_Statement, "insert_id", 0, sl_mysql_stmt_insert_id); vm->store[cMySQL] = MySQL; vm->store[cMySQL_Error] = MySQL_Error; vm->store[cMySQL_Statement] = MySQL_Statement; sl_do_string(vm, (uint8_t*)sl__ext_mysql_extensions_sl, strlen(sl__ext_mysql_extensions_sl), "ext/mysql/extensions.sl", 0); }
SLVAL sl_do_file(sl_vm_t* vm, char* filename) { filename = sl_realpath(vm, filename); FILE* f = fopen(filename, "rb"); uint8_t* src; size_t file_size; SLVAL err; if(!f) { err = sl_make_cstring(vm, "Could not load file: "); err = sl_string_concat(vm, err, sl_make_cstring(vm, filename)); err = sl_string_concat(vm, err, sl_make_cstring(vm, " - ")); err = sl_string_concat(vm, err, sl_make_cstring(vm, strerror(errno))); sl_throw(vm, sl_make_error2(vm, vm->lib.Error, err)); } fseek(f, 0, SEEK_END); file_size = ftell(f); fseek(f, 0, SEEK_SET); src = sl_alloc(vm->arena, file_size); if(file_size && !fread(src, file_size, 1, f)) { fclose(f); err = sl_make_cstring(vm, "Could not load file: "); err = sl_string_concat(vm, err, sl_make_cstring(vm, filename)); err = sl_string_concat(vm, err, sl_make_cstring(vm, " - ")); err = sl_string_concat(vm, err, sl_make_cstring(vm, strerror(errno))); sl_throw(vm, sl_make_error2(vm, vm->lib.Error, err)); } fclose(f); return sl_do_string(vm, src, file_size, filename, 0); }
static void run_repl(sl_vm_t* vm) { printf("Interactive Slash\n"); cli_setup_readline(); while(true) { char* line = cli_readline(">> "); if(!line) { printf("\n"); shutdown_vm(vm, 0); } sl_vm_frame_t frame; SLVAL exception; SL_TRY(frame, SL_UNWIND_EXCEPTION, { SLVAL result = sl_do_string(vm, (uint8_t*)line, strlen(line), "(repl)", 1); result = sl_inspect(vm, result); printf("=> "); sl_string_t* str = (sl_string_t*)sl_get_ptr(result); fwrite(str->buff, str->buff_len, 1, stdout); printf("\n"); }, exception, { SLVAL exception_str = sl_to_s_no_throw(vm, exception); sl_string_t* str = (sl_string_t*)sl_get_ptr(exception_str); fwrite(str->buff, str->buff_len, 1, stdout); printf("\n"); });
void sl_render_error_page(sl_vm_t* vm, SLVAL err) { sl_vm_frame_t frame; sl_response_internal_opts_t* resp = response(vm); resp->status = 500; SLVAL caught_error; if(resp->descriptive_error_pages) { SL_TRY(frame, SL_UNWIND_EXCEPTION, { SLVAL error_lambda = sl_do_string(vm, (uint8_t*)sl__src_lib_error_page_sl, strlen(sl__src_lib_error_page_sl), "(error-page)", 0); sl_lambda_call(vm, error_lambda, 1, &err); }, caught_error, {
static SLVAL sl_do_file_hashbang(sl_vm_t* vm, char* filename, int skip_hashbang) { FILE* fh; uint8_t* src; long file_size; size_t real_start = 0; if(!skip_hashbang) { return sl_do_file(vm, filename); } filename = sl_realpath(vm, filename); fh = fopen(filename, "rb"); if(!fh) { sl_error(vm, vm->lib.Error, "Could not load %Qs - %s", filename, strerror(errno)); } fseek(fh, 0, SEEK_END); file_size = ftell(fh); fseek(fh, 0, SEEK_SET); if(file_size < 0) { sl_error(vm, vm->lib.Error, "Could not load %Qs - %s (count not get filesize)", filename, strerror(errno)); } src = sl_alloc(vm->arena, file_size); if(file_size && !fread(src, file_size, 1, fh)) { fclose(fh); sl_error(vm, vm->lib.Error, "Could not load %Qs - %s", filename, strerror(errno)); } fclose(fh); if(skip_hashbang) { real_start = get_hashbang_length(src, file_size); } return sl_do_string(vm, src + real_start, file_size - real_start, filename, 0); }