JSValueRef function_cache(JSContextRef ctx, JSObjectRef function, JSObjectRef thisObject, size_t argc, const JSValueRef args[], JSValueRef *exception) { if (argc == 4 && JSValueGetType(ctx, args[0]) == kJSTypeString && JSValueGetType(ctx, args[1]) == kJSTypeString && (JSValueGetType(ctx, args[2]) == kJSTypeString || JSValueGetType(ctx, args[2]) == kJSTypeNull) && (JSValueGetType(ctx, args[3]) == kJSTypeString || JSValueGetType(ctx, args[3]) == kJSTypeNull)) { // debug_print_value("cache", ctx, args[0]); char *cache_prefix = value_to_c_string(ctx, args[0]); char *source = value_to_c_string(ctx, args[1]); char *cache = value_to_c_string(ctx, args[2]); char *sourcemap = value_to_c_string(ctx, args[3]); char *suffix = NULL; int max_suffix_len = 20; size_t prefix_len = strlen(cache_prefix); char *path = malloc((prefix_len + max_suffix_len) * sizeof(char)); memset(path, 0, prefix_len + max_suffix_len); suffix = ".js"; strcpy(path, cache_prefix); strcat(path, suffix); write_contents(path, source); suffix = ".cache.json"; strcpy(path, cache_prefix); strcat(path, suffix); write_contents(path, cache); suffix = ".js.map.json"; strcpy(path, cache_prefix); strcat(path, suffix); if (sourcemap) { write_contents(path, sourcemap); } free(cache_prefix); free(source); free(cache); free(sourcemap); free(path); } return JSValueMakeNull(ctx); }
JSValueRef function_file_output_stream_write(JSContextRef ctx, JSObjectRef function, JSObjectRef thisObject, size_t argc, const JSValueRef args[], JSValueRef *exception) { if (argc == 2 && JSValueGetType(ctx, args[0]) == kJSTypeString && JSValueGetType(ctx, args[1]) == kJSTypeObject) { char *descriptor = value_to_c_string(ctx, args[0]); unsigned int count = (unsigned int) array_get_count(ctx, (JSObjectRef) args[1]); uint8_t buf[count]; for (unsigned int i = 0; i < count; i++) { JSValueRef v = array_get_value_at_index(ctx, (JSObjectRef) args[1], i); if (JSValueIsNumber(ctx, v)) { double n = JSValueToNumber(ctx, v, NULL); if (0 <= n && n <= 255) { buf[i] = (uint8_t) n; } else { fprintf(stderr, "Output stream value out of range %f", n); } } else { fprintf(stderr, "Output stream value not a number"); } } file_write(descriptor_str_to_int(descriptor), count, buf); free(descriptor); } return JSValueMakeNull(ctx); }
JSValueRef function_file_input_stream_read(JSContextRef ctx, JSObjectRef function, JSObjectRef thisObject, size_t argc, const JSValueRef args[], JSValueRef *exception) { if (argc == 1 && JSValueGetType(ctx, args[0]) == kJSTypeString) { char *descriptor = value_to_c_string(ctx, args[0]); size_t buf_size = 4096; uint8_t *buf = malloc(buf_size * sizeof(uint8_t)); size_t read = file_read(descriptor_str_to_int(descriptor), buf_size, buf); free(descriptor); JSValueRef arguments[read]; int num_arguments = (int) read; for (int i = 0; i < num_arguments; i++) { arguments[i] = JSValueMakeNumber(ctx, buf[i]); } return JSObjectMakeArray(ctx, num_arguments, arguments, NULL); } return JSValueMakeNull(ctx); }
void print_value(char *prefix, JSContextRef ctx, JSValueRef val) { if (val != NULL) { JSStringRef str = to_string(ctx, val); char *ex_str = value_to_c_string(ctx, JSValueMakeString(ctx, str)); printf("%s%s\n", prefix, ex_str); free(ex_str); } }
JSValueRef function_raw_write_stderr(JSContextRef ctx, JSObjectRef function, JSObjectRef thisObject, size_t argc, const JSValueRef args[], JSValueRef *exception) { if (argc == 1 && JSValueGetType(ctx, args[0]) == kJSTypeString) { char *s = value_to_c_string(ctx, args[0]); fprintf(stderr, "%s", s); free(s); } return JSValueMakeNull(ctx); }
JSValueRef function_file_output_stream_close(JSContextRef ctx, JSObjectRef function, JSObjectRef thisObject, size_t argc, const JSValueRef args[], JSValueRef *exception) { if (argc == 1 && JSValueGetType(ctx, args[0]) == kJSTypeString) { char *descriptor = value_to_c_string(ctx, args[0]); file_close(descriptor_str_to_int(descriptor)); free(descriptor); } return JSValueMakeNull(ctx); }
JSValueRef function_delete_file(JSContextRef ctx, JSObjectRef function, JSObjectRef thisObject, size_t argc, const JSValueRef args[], JSValueRef *exception) { if (argc == 1 && JSValueGetType(ctx, args[0]) == kJSTypeString) { char *path = value_to_c_string(ctx, args[0]); remove(path); free(path); } return JSValueMakeNull(ctx); }
JSValueRef function_file_reader_open(JSContextRef ctx, JSObjectRef function, JSObjectRef thisObject, size_t argc, const JSValueRef args[], JSValueRef *exception) { if (argc == 2 && JSValueGetType(ctx, args[0]) == kJSTypeString) { char *path = value_to_c_string(ctx, args[0]); char *encoding = value_to_c_string(ctx, args[1]); uint64_t descriptor = ufile_open_read(path, encoding); free(path); free(encoding); char *descriptor_str = descriptor_int_to_str(descriptor); JSValueRef rv = c_string_to_value(ctx, descriptor_str); free(descriptor_str); return rv; } return JSValueMakeNull(ctx); }
JSValueRef function_print_err_fn(JSContextRef ctx, JSObjectRef function, JSObjectRef thisObject, size_t argc, const JSValueRef args[], JSValueRef* exception) { if (argc == 1 && JSValueIsString(ctx, args[0])) { char *str = value_to_c_string(ctx, args[0]); fprintf(stderr, "%s", str); fflush(stderr); free(str); } return JSValueMakeNull(ctx); }
JSValueRef function_list_files(JSContextRef ctx, JSObjectRef function, JSObjectRef thisObject, size_t argc, const JSValueRef args[], JSValueRef *exception) { if (argc == 1 && JSValueGetType(ctx, args[0]) == kJSTypeString) { char *path = value_to_c_string(ctx, args[0]); size_t capacity = 32; size_t count = 0; JSValueRef *paths = malloc(capacity * sizeof(paths)); DIR *d; struct dirent *dir; d = opendir(path); size_t path_len = strlen(path); if (path_len && path[path_len - 1] == '/') { path[--path_len] = 0; } if (d) { while ((dir = readdir(d)) != NULL) { if (strcmp(dir->d_name, ".") && strcmp(dir->d_name, "..")) { char *buf = malloc((path_len + strlen(dir->d_name) + 2)); sprintf(buf, "%s/%s", path, dir->d_name); paths[count++] = c_string_to_value(ctx, buf); free(buf); if (count == capacity) { capacity *= 2; paths = realloc(paths, capacity * sizeof(paths)); } } } closedir(d); } free(path); JSValueRef rv = JSObjectMakeArray(ctx, count, paths, NULL); free(paths); return rv; } return JSValueMakeNull(ctx); }
JSValueRef function_file_writer_write(JSContextRef ctx, JSObjectRef function, JSObjectRef thisObject, size_t argc, const JSValueRef args[], JSValueRef *exception) { if (argc == 2 && JSValueGetType(ctx, args[0]) == kJSTypeString && JSValueGetType(ctx, args[1]) == kJSTypeString) { char *descriptor = value_to_c_string(ctx, args[0]); JSStringRef str_ref = JSValueToStringCopy(ctx, args[1], NULL); ufile_write(descriptor_str_to_int(descriptor), str_ref); free(descriptor); JSStringRelease(str_ref); } return JSValueMakeNull(ctx); }
JSValueRef function_print_fn(JSContextRef ctx, JSObjectRef function, JSObjectRef thisObject, size_t argc, const JSValueRef args[], JSValueRef *exception) { if (!should_keep_running()) { fprintf(stdout, "\x1b[m\n"); fflush(stdout); *exception = JSValueMakeNull(ctx); return NULL; } if (argc == 1 && JSValueIsString(ctx, args[0])) { char *str = value_to_c_string(ctx, args[0]); fprintf(stdout, "%s", str); fflush(stdout); free(str); } return JSValueMakeNull(ctx); }
JSValueRef function_file_output_stream_open(JSContextRef ctx, JSObjectRef function, JSObjectRef thisObject, size_t argc, const JSValueRef args[], JSValueRef *exception) { if (argc == 2 && JSValueGetType(ctx, args[0]) == kJSTypeString && JSValueGetType(ctx, args[1]) == kJSTypeBoolean) { char *path = value_to_c_string(ctx, args[0]); bool append = JSValueToBoolean(ctx, args[1]); uint64_t descriptor = file_open_write(path, append); free(path); char *descriptor_str = descriptor_int_to_str(descriptor); JSValueRef rv = c_string_to_value(ctx, descriptor_str); free(descriptor_str); return rv; } return JSValueMakeNull(ctx); }
JSValueRef function_read_password(JSContextRef ctx, JSObjectRef function, JSObjectRef thisObject, size_t argc, const JSValueRef args[], JSValueRef *exception) { if (argc == 1 && JSValueGetType(ctx, args[0]) == kJSTypeString) { char *prompt = value_to_c_string(ctx, args[0]); char *pass = getpass(prompt); JSValueRef rv; if (pass) { rv = c_string_to_value(ctx, pass); memset(pass, 0, strlen(pass)); } else { rv = JSValueMakeNull(ctx); } free(prompt); return rv; } return JSValueMakeNull(ctx); }
JSValueRef function_is_directory(JSContextRef ctx, JSObjectRef function, JSObjectRef thisObject, size_t argc, const JSValueRef args[], JSValueRef *exception) { if (argc == 1 && JSValueGetType(ctx, args[0]) == kJSTypeString) { char *path = value_to_c_string(ctx, args[0]); bool is_directory = false; struct stat file_stat; int retval = stat(path, &file_stat); free(path); if (retval == 0) { is_directory = S_ISDIR(file_stat.st_mode); } return JSValueMakeBoolean(ctx, is_directory); } return JSValueMakeNull(ctx); }
JSValueRef function_file_reader_read(JSContextRef ctx, JSObjectRef function, JSObjectRef thisObject, size_t argc, const JSValueRef args[], JSValueRef *exception) { if (argc == 1 && JSValueGetType(ctx, args[0]) == kJSTypeString) { char *descriptor = value_to_c_string(ctx, args[0]); JSStringRef result = ufile_read(descriptor_str_to_int(descriptor)); free(descriptor); JSValueRef arguments[2]; if (result != NULL) { arguments[0] = JSValueMakeString(ctx, result); JSStringRelease(result); } else { arguments[0] = JSValueMakeNull(ctx); } arguments[1] = JSValueMakeNull(ctx); return JSObjectMakeArray(ctx, 2, arguments, NULL); } return JSValueMakeNull(ctx); }
JSValueRef function_http_request(JSContextRef ctx, JSObjectRef function, JSObjectRef this_object, size_t argc, const JSValueRef args[], JSValueRef* exception) { if (argc == 1 && JSValueGetType(ctx, args[0]) == kJSTypeObject) { JSObjectRef opts = JSValueToObject(ctx, args[0], NULL); JSValueRef url_ref = JSObjectGetProperty(ctx, opts, JSStringCreateWithUTF8CString("url"), NULL); char *url = value_to_c_string(ctx, url_ref); JSValueRef timeout_ref = JSObjectGetProperty(ctx, opts, JSStringCreateWithUTF8CString("timeout"), NULL); time_t timeout = 0; if (JSValueIsNumber(ctx, timeout_ref)) { timeout = (time_t) JSValueToNumber(ctx, timeout_ref, NULL); } JSValueRef method_ref = JSObjectGetProperty(ctx, opts, JSStringCreateWithUTF8CString("method"), NULL); char *method = value_to_c_string(ctx, method_ref); JSValueRef body_ref = JSObjectGetProperty(ctx, opts, JSStringCreateWithUTF8CString("body"), NULL); JSObjectRef headers_obj = JSValueToObject(ctx, JSObjectGetProperty(ctx, opts, JSStringCreateWithUTF8CString("headers"), NULL), NULL); CURL *handle = curl_easy_init(); assert(handle != NULL); curl_easy_setopt(handle, CURLOPT_CUSTOMREQUEST, method); curl_easy_setopt(handle, CURLOPT_URL, url); struct curl_slist *headers = NULL; if (!JSValueIsNull(ctx, headers_obj)) { JSPropertyNameArrayRef properties = JSObjectCopyPropertyNames(ctx, headers_obj); size_t n = JSPropertyNameArrayGetCount(properties); for (int i = 0; i < n; i++) { JSStringRef key_str = JSPropertyNameArrayGetNameAtIndex(properties, i); JSValueRef val_ref = JSObjectGetProperty(ctx, headers_obj, key_str, NULL); size_t len = JSStringGetLength(key_str) + 1; char *key = malloc(len * sizeof(char)); JSStringGetUTF8CString(key_str, key, len); JSStringRef val_as_str = to_string(ctx, val_ref); char *val = value_to_c_string(ctx, JSValueMakeString(ctx, val_as_str)); JSStringRelease(val_as_str); size_t len_key = strlen(key); size_t len_val = strlen(val); char *header = malloc((len_key + len_val + 2 + 1) * sizeof(char)); sprintf(header, "%s: %s", key, val); curl_slist_append(headers, header); free(header); free(key); free(val); } curl_easy_setopt(handle, CURLOPT_HEADER, headers); } // curl_easy_setopt(handle, CURLOPT_HEADER, 1L); curl_easy_setopt(handle, CURLOPT_TIMEOUT, timeout); struct read_string_state input_state; if (!JSValueIsUndefined(ctx, body_ref)) { char *body = value_to_c_string(ctx, body_ref); input_state.input = body; input_state.offset = 0; input_state.length = strlen(body); curl_easy_setopt(handle, CURLOPT_READDATA, &input_state); curl_easy_setopt(handle, CURLOPT_READFUNCTION, read_string_callback); } JSObjectRef response_headers = JSObjectMake(ctx, NULL, NULL); struct header_state header_state; header_state.ctx = ctx; header_state.headers = response_headers; curl_easy_setopt(handle, CURLOPT_HEADERDATA, &header_state); curl_easy_setopt(handle, CURLOPT_HEADERFUNCTION, header_to_object_callback); struct write_state body_state; body_state.offset = 0; body_state.length = 0; body_state.data = NULL; curl_easy_setopt(handle, CURLOPT_WRITEDATA, &body_state); curl_easy_setopt(handle, CURLOPT_WRITEFUNCTION, write_string_callback); JSObjectRef result = JSObjectMake(ctx, NULL, NULL); int res = curl_easy_perform(handle); if (res != 0) { JSStringRef error_str = JSStringCreateWithUTF8CString(curl_easy_strerror(res)); JSObjectSetProperty(ctx, result, JSStringCreateWithUTF8CString("error"), JSValueMakeString(ctx, error_str), kJSPropertyAttributeReadOnly, NULL); } int status = 0; curl_easy_getinfo(handle, CURLINFO_RESPONSE_CODE, &status); // printf("%d bytes, %x\n", body_state.offset, body_state.data); if (body_state.data != NULL) { JSStringRef body_str = JSStringCreateWithUTF8CString(body_state.data); JSObjectSetProperty(ctx, result, JSStringCreateWithUTF8CString("body"), JSValueMakeString(ctx, body_str), kJSPropertyAttributeReadOnly, NULL); free(body_state.data); } JSObjectSetProperty(ctx, result, JSStringCreateWithUTF8CString("status"), JSValueMakeNumber(ctx, status), kJSPropertyAttributeReadOnly, NULL); JSObjectSetProperty(ctx, result, JSStringCreateWithUTF8CString("headers"), response_headers, kJSPropertyAttributeReadOnly, NULL); curl_slist_free_all(headers); curl_easy_cleanup(handle); return result; } return JSValueMakeNull(ctx); }
JSValueRef function_fstat(JSContextRef ctx, JSObjectRef function, JSObjectRef thisObject, size_t argc, const JSValueRef args[], JSValueRef *exception) { if (argc == 1 && JSValueGetType(ctx, args[0]) == kJSTypeString) { char *path = value_to_c_string(ctx, args[0]); struct stat file_stat; int retval = lstat(path, &file_stat); if (retval == 0) { JSObjectRef result = JSObjectMake(ctx, NULL, NULL); char *type = "unknown"; if (S_ISDIR(file_stat.st_mode)) { type = "directory"; } else if (S_ISREG(file_stat.st_mode)) { type = "file"; } else if (S_ISLNK(file_stat.st_mode)) { type = "symbolic-link"; } else if (S_ISSOCK(file_stat.st_mode)) { type = "socket"; } else if (S_ISFIFO(file_stat.st_mode)) { type = "fifo"; } else if (S_ISCHR(file_stat.st_mode)) { type = "character-special"; } else if (S_ISBLK(file_stat.st_mode)) { type = "block-special"; } JSObjectSetProperty(ctx, result, JSStringCreateWithUTF8CString("type"), c_string_to_value(ctx, type), kJSPropertyAttributeReadOnly, NULL); double device_id = (double) file_stat.st_rdev; if (device_id) { JSObjectSetProperty(ctx, result, JSStringCreateWithUTF8CString("device-id"), JSValueMakeNumber(ctx, device_id), kJSPropertyAttributeReadOnly, NULL); } double file_number = (double) file_stat.st_ino; if (file_number) { JSObjectSetProperty(ctx, result, JSStringCreateWithUTF8CString("file-number"), JSValueMakeNumber(ctx, file_number), kJSPropertyAttributeReadOnly, NULL); } JSObjectSetProperty(ctx, result, JSStringCreateWithUTF8CString("permissions"), JSValueMakeNumber(ctx, (double) (ACCESSPERMS & file_stat.st_mode)), kJSPropertyAttributeReadOnly, NULL); JSObjectSetProperty(ctx, result, JSStringCreateWithUTF8CString("reference-count"), JSValueMakeNumber(ctx, (double) file_stat.st_nlink), kJSPropertyAttributeReadOnly, NULL); JSObjectSetProperty(ctx, result, JSStringCreateWithUTF8CString("uid"), JSValueMakeNumber(ctx, (double) file_stat.st_uid), kJSPropertyAttributeReadOnly, NULL); struct passwd *uid_passwd = getpwuid(file_stat.st_uid); if (uid_passwd) { JSObjectSetProperty(ctx, result, JSStringCreateWithUTF8CString("uname"), c_string_to_value(ctx, uid_passwd->pw_name), kJSPropertyAttributeReadOnly, NULL); } JSObjectSetProperty(ctx, result, JSStringCreateWithUTF8CString("gid"), JSValueMakeNumber(ctx, (double) file_stat.st_gid), kJSPropertyAttributeReadOnly, NULL); struct group *gid_group = getgrgid(file_stat.st_gid); if (gid_group) { JSObjectSetProperty(ctx, result, JSStringCreateWithUTF8CString("gname"), c_string_to_value(ctx, gid_group->gr_name), kJSPropertyAttributeReadOnly, NULL); } JSObjectSetProperty(ctx, result, JSStringCreateWithUTF8CString("file-size"), JSValueMakeNumber(ctx, (double) file_stat.st_size), kJSPropertyAttributeReadOnly, NULL); #ifdef __APPLE__ #define birthtime(x) x.st_birthtime #else #define birthtime(x) x.st_ctime #endif JSObjectSetProperty(ctx, result, JSStringCreateWithUTF8CString("created"), JSValueMakeNumber(ctx, 1000 * birthtime(file_stat)), kJSPropertyAttributeReadOnly, NULL); JSObjectSetProperty(ctx, result, JSStringCreateWithUTF8CString("modified"), JSValueMakeNumber(ctx, 1000 * file_stat.st_mtime), kJSPropertyAttributeReadOnly, NULL); return result; } free(path); } return JSValueMakeNull(ctx); }