void sl_init_float(sl_vm_t* vm) { vm->lib.Float = sl_define_class(vm, "Float", vm->lib.Number); sl_class_set_allocator(vm, vm->lib.Float, allocate_float); sl_define_method(vm, vm->lib.Float, "to_s", 0, sl_float_to_s); sl_define_method(vm, vm->lib.Float, "inspect", 0, sl_float_to_s); sl_define_method(vm, vm->lib.Float, "to_i", 0, sl_float_to_i); sl_define_method(vm, vm->lib.Float, "to_f", 0, sl_float_to_f); sl_define_method(vm, vm->lib.Float, "succ", 0, sl_float_succ); sl_define_method(vm, vm->lib.Float, "pred", 0, sl_float_pred); sl_define_method(vm, vm->lib.Float, "-self", 0, sl_float_negate); sl_define_method(vm, vm->lib.Float, "round", 0, sl_float_round); sl_define_method(vm, vm->lib.Float, "floor", 0, sl_float_floor); sl_define_method(vm, vm->lib.Float, "ceil", 0, sl_float_ceil); sl_define_method(vm, vm->lib.Float, "+", 1, sl_float_add); sl_define_method(vm, vm->lib.Float, "-", 1, sl_float_sub); sl_define_method(vm, vm->lib.Float, "*", 1, sl_float_mul); sl_define_method(vm, vm->lib.Float, "/", 1, sl_float_div); sl_define_method(vm, vm->lib.Float, "%", 1, sl_float_mod); sl_define_method(vm, vm->lib.Float, "**", 1, sl_float_pow); sl_define_method(vm, vm->lib.Float, "==", 1, sl_float_eq); sl_define_method(vm, vm->lib.Float, "<=>", 1, sl_float_cmp); sl_define_method(vm, vm->lib.Float, "nan", 0, sl_float_nan); sl_define_method(vm, vm->lib.Float, "finite", 0, sl_float_finite); sl_define_method(vm, vm->lib.Float, "infinite", 0, sl_float_infinite); sl_class_set_const(vm, vm->lib.Object, "Infinity", sl_make_float(vm, 1.0/0.0)); sl_class_set_const(vm, vm->lib.Object, "NaN", sl_make_float(vm, 0.0/0.0)); }
void sl_init_regexp(sl_vm_t* vm) { vm->lib.Regexp = sl_define_class(vm, "Regexp", vm->lib.Object); sl_class_set_allocator(vm, vm->lib.Regexp, allocate_regexp); vm->lib.Regexp_Match = sl_define_class3(vm, sl_intern(vm, "Match"), vm->lib.Object, vm->lib.Regexp); sl_class_set_allocator(vm, vm->lib.Regexp_Match, allocate_regexp_match); sl_define_method(vm, vm->lib.Regexp, "init", -2, sl_regexp_init); /* sl_define_method(vm, vm->lib.Regexp, "compile", 0, sl_regexp_compile); */ sl_define_method(vm, vm->lib.Regexp, "match", -2, sl_regexp_match); sl_define_method(vm, vm->lib.Regexp, "source", 0, sl_regexp_source); sl_define_method(vm, vm->lib.Regexp, "options", 0, sl_regexp_options); sl_define_method(vm, vm->lib.Regexp, "==", 1, sl_regexp_eq); sl_class_set_const(vm, vm->lib.Regexp, "CASELESS", sl_make_int(vm, PCRE_CASELESS)); sl_class_set_const(vm, vm->lib.Regexp, "EXTENDED", sl_make_int(vm, PCRE_EXTENDED)); sl_class_set_const(vm, vm->lib.Regexp, "PCRE_VERSION", sl_make_cstring(vm, pcre_version())); sl_define_method(vm, vm->lib.Regexp_Match, "regexp", 0, sl_regexp_match_regexp); sl_define_method(vm, vm->lib.Regexp_Match, "[]", 1, sl_regexp_match_index); sl_define_method(vm, vm->lib.Regexp_Match, "byte_offset", 1, sl_regexp_match_byte_offset); sl_define_method(vm, vm->lib.Regexp_Match, "offset", 1, sl_regexp_match_offset); sl_define_method(vm, vm->lib.Regexp_Match, "capture", 1, sl_regexp_match_capture); sl_define_method(vm, vm->lib.Regexp_Match, "length", 0, sl_regexp_match_length); sl_define_method(vm, vm->lib.Regexp_Match, "before", 0, sl_regexp_match_before); sl_define_method(vm, vm->lib.Regexp_Match, "after", 0, sl_regexp_match_after); }
static void process_arguments(sl_vm_t* vm, int argc, char** argv) { int i = 1; for(; i < argc; i++) { if(strcmp(argv[i], "-I") == 0) { if(i + 1 == argc) { fprintf(stderr, "Expected <path> after -I\n"); shutdown_vm(vm, 1); } sl_require_path_prepend(vm, argv[++i]); continue; } if(strcmp(argv[i], "-i") == 0) { opt_interactive = true; continue; } if(strcmp(argv[i], "-h") == 0) { print_help(argv[0]); shutdown_vm(vm, 0); } if(strcmp(argv[i], "--") == 0) { i++; break; } if(argv[i][0] == '-') { fprintf(stderr, "Unknown option '%s'\n", argv[i]); shutdown_vm(vm, 1); } break; } if(!opt_interactive) { if(i == argc || strcmp(argv[i], "-") == 0) { opt_source_file = stdin; opt_source_file_name = "(stdin)"; } else { opt_source_file = fopen(argv[i], "rb"); opt_source_file_name = argv[i]; if(!opt_source_file) { perror("Could not open source file"); shutdown_vm(vm, 1); } } i++; } SLVAL vargv = sl_make_array(vm, 0, NULL); for(; i < argc; i++) { SLVAL varg = sl_make_cstring(vm, argv[i]); sl_array_push(vm, vargv, 1, &varg); } sl_class_set_const(vm, vm->lib.Object, "ARGV", vargv); }
static void load_cgi_options(sl_vm_t* vm, cgi_options* options) { int i = 0; for(; options->incpaths && i < options->incpaths_count; i++) { sl_require_path_prepend(vm, options->incpaths[i]); } SLVAL vargv = sl_make_array(vm, 0, NULL); for(i = 0; i < options->arg_script_options_count; i++) { SLVAL varg = sl_make_cstring(vm, options->arg_script_options[i]); sl_array_push(vm, vargv, 1, &varg); } sl_class_set_const(vm, vm->lib.Object, "ARGV", vargv); }
void sl_init_request(sl_vm_t* vm) { SLVAL Request = sl_new(vm, vm->lib.Object, 0, NULL); sl_vm_store_put(vm, &Request_, Request); sl_define_singleton_method(vm, Request, "get", 0, request_get); sl_define_singleton_method(vm, Request, "post", 0, request_post); sl_define_singleton_method(vm, Request, "post_data", 0, request_post_data); sl_define_singleton_method(vm, Request, "headers", 0, request_headers); sl_define_singleton_method(vm, Request, "env", 0, request_env); sl_define_singleton_method(vm, Request, "cookies", 0, request_cookies); sl_define_singleton_method(vm, Request, "method", 0, request_method); sl_define_singleton_method(vm, Request, "safe_method", 0, request_safe_method); sl_define_singleton_method(vm, Request, "uri", 0, request_uri); sl_define_singleton_method(vm, Request, "path_info", 0, request_path_info); sl_define_singleton_method(vm, Request, "query_string", 0, request_query_string); sl_define_singleton_method(vm, Request, "remote_addr", 0, request_remote_addr); sl_define_singleton_method(vm, Request, "[]", 1, request_index); sl_class_set_const(vm, vm->lib.Object, "Request", Request); }