Beispiel #1
0
static ejsval
_ejs_fs_readFileSync (ejsval env, ejsval _this, uint32_t argc, ejsval* args)
{
    // FIXME we currently ignore the encoding and just slam the entire thing into a buffer and return a utf8 string...
    char* utf8_path = ucs2_to_utf8(EJSVAL_TO_FLAT_STRING(args[0]));

    int fd = open (utf8_path, O_RDONLY);
    if (fd == -1) {
        char buf[256];
        snprintf (buf, sizeof(buf), "%s: `%s`", strerror(errno), utf8_path);
        free(utf8_path);
        _ejs_throw_nativeerror_utf8 (EJS_ERROR, buf);
    }

    struct stat fd_stat;

    int stat_rv = fstat (fd, &fd_stat);
    if (stat_rv == -1) {
        char buf[256];
        snprintf (buf, sizeof(buf), "%s: `%s`", strerror(errno), utf8_path);
        free(utf8_path);
        close(fd);
        _ejs_throw_nativeerror_utf8 (EJS_ERROR, buf);
    }

    int amount_to_read = fd_stat.st_size;
    int amount_read = 0;
    char *buf = (char*)calloc (1, amount_to_read+1);
    do {
        int c = read(fd, buf + amount_read, amount_to_read);
        if (c == -1) {
            if (errno == EINTR)
                continue;
            else {
                char msg[256];
                snprintf (msg, sizeof(msg), "%s: `%s`", strerror(errno), utf8_path);
                free(utf8_path);
                close(fd);
                free (buf);
                _ejs_throw_nativeerror_utf8 (EJS_ERROR, msg);
            }
        }
        else {
            amount_to_read -= c;
            amount_read += c;
        }
    } while (amount_to_read > 0);

    free(utf8_path);

    ejsval rv = _ejs_string_new_utf8_len(buf, amount_read);
    free(buf);
    return rv;
}
Beispiel #2
0
static ejsval
_ejs_fs_readFileSync (ejsval env, ejsval _this, uint32_t argc, ejsval* args)
{
    // FIXME we currently ignore the encoding and just slam the entire thing into a buffer and return a utf8 string...
    char* utf8_path = ucs2_to_utf8(EJSVAL_TO_FLAT_STRING(args[0]));

    int fd = open (utf8_path, O_RDONLY);
    free(utf8_path);

    struct stat fd_stat;

    fstat (fd, &fd_stat);

    char *buf = (char*)malloc (fd_stat.st_size);
    read(fd, buf, fd_stat.st_size);
    close(fd);

    ejsval rv = _ejs_string_new_utf8_len(buf, fd_stat.st_size);
    free(buf);
    return rv;
}
Beispiel #3
0
static EJS_NATIVE_FUNC(_ejs_Process_get_env) {
    ejsval env_obj = _ejs_object_new(_ejs_null, &_ejs_Object_specops);

    char** p = environ;

    while (*p) {
        char *env_entry = *p;
        char *eq = strchr(env_entry, '=');
        if (!eq) {
            p++;
            continue;
        }

        ejsval k = _ejs_string_new_utf8_len(env_entry, eq - env_entry);
        ejsval v = _ejs_string_new_utf8(eq+1);

        _ejs_object_define_value_property (env_obj, k, v, EJS_PROP_ENUMERABLE | EJS_PROP_NOT_CONFIGURABLE | EJS_PROP_NOT_WRITABLE);
        p++;
    }

    return env_obj;
}
Beispiel #4
0
void allinone_for_load_just_ensure_these_functions_and_variables_are_included_please_do_not_call()
{
    JSValueHash(_ejs_nan);
    JSValueHash(jsPositiveInfinity);
    JSValueHash(jsNegativeInfinity);
    JSValueHash(jsMax);
    JSValueHash(jsMin);
    JSValueHash(_ejs_null);
    JSValueHash(_ejs_undefined);
    JSValueHash(_ejs_true);
    JSValueHash(_ejs_false);
    JSValueHash(_ejs_one);
    JSValueHash(_ejs_zero);
    JSValueHash(_ejs_global);
    JSValueHash(_ejs_console);
    JSValueHash(_ejs_Object);
    JSValueHash(_ejs_Boolean);
    JSValueHash(_ejs_Number);
    JSValueHash(_ejs_String);
    JSValueHash(_ejs_Array);
    JSValueHash(_ejs_Function);
    JSValueHash(_ejs_Process);
    JSValueHash(_ejs_Symbol_create);
    JSValueHash(_ejs_Math);
    JSValueHash(_ejs_JSON);

    jsextern_print_tick();
    jsextern_os_msleep(0);
    jsextern_os_swap(NULL, 0, 0);
    jsextern_pcre_compile(NULL);
    jsextern_pcre_study(NULL);
    jsextern_pcre_bracketcount(NULL);
    jsextern_pcre_exec(NULL, NULL, NULL, 0, 0, NULL, 0);
    jsextern_pcre_free(NULL);
    jsextern_thread_create((void *(*)(void *))NULL, NULL);
    jsextern_thread_destroy(0);
    jsextern_mutex_create();
    jsextern_mutex_destroy(0);
    jsextern_mutex_lock(0);
    jsextern_mutex_unlock(0);
    jsextern_signal_create();
    jsextern_signal_destroy(0);
    jsextern_signal_wait(0);
    jsextern_signal_send(0);

    JSValueHash(_ejs_undefined);
    
    _ejs_eval(_ejs_undefined, _ejs_undefined, 0, NULL);

    //Object
    _ejs_object_getprop_utf8(_ejs_undefined, NULL);
    _ejs_object_setprop_utf8(_ejs_global, NULL, _ejs_undefined);
    _ejs_object_define_value_property(_ejs_undefined, _ejs_undefined, _ejs_undefined, 0);
    _ejs_object_define_getter_property(_ejs_undefined, _ejs_undefined, _ejs_undefined, 0);
    _ejs_object_define_setter_property(_ejs_undefined, _ejs_undefined, _ejs_undefined, 0);
    _ejs_Object_create(_ejs_undefined, _ejs_undefined, 0, NULL);
    _ejs_Object_getOwnPropertyNames(_ejs_undefined, _ejs_undefined, 0, NULL);
    //vtable
    _ejs_specop_get(_ejs_undefined, _ejs_undefined, _ejs_undefined);
    _ejs_specop_set(_ejs_undefined, _ejs_undefined, _ejs_undefined, _ejs_undefined);
    //option
    ToEJSBool(_ejs_undefined);
    ToDouble(_ejs_undefined);
    ToUint32(_ejs_undefined);
    _ejs_op_typeof(_ejs_undefined);
    _ejs_op_instanceof(_ejs_undefined, _ejs_undefined);
    _ejs_op_typeof_is_array(_ejs_undefined);
    _ejs_op_plusplus(_ejs_undefined, EJS_FALSE);
    _ejs_op_minusminus(_ejs_undefined, EJS_FALSE);
    _ejs_op_bitwise_xor(_ejs_undefined, _ejs_undefined);
    _ejs_op_bitwise_and(_ejs_undefined, _ejs_undefined);
    _ejs_op_bitwise_or(_ejs_undefined, _ejs_undefined);
    _ejs_op_rsh(_ejs_undefined, _ejs_undefined);
    _ejs_op_ursh(_ejs_undefined, _ejs_undefined);
    _ejs_op_lsh(_ejs_undefined, _ejs_undefined);
    _ejs_op_ulsh(_ejs_undefined, _ejs_undefined);
    _ejs_op_mod(_ejs_undefined, _ejs_undefined);
    _ejs_op_add(_ejs_undefined, _ejs_undefined);
    _ejs_op_sub(_ejs_undefined, _ejs_undefined);
    _ejs_op_mult(_ejs_undefined, _ejs_undefined);
    _ejs_op_div(_ejs_undefined, _ejs_undefined);
    _ejs_op_lt(_ejs_undefined, _ejs_undefined);
    _ejs_op_le(_ejs_undefined, _ejs_undefined);
    _ejs_op_gt(_ejs_undefined, _ejs_undefined);
    _ejs_op_ge(_ejs_undefined, _ejs_undefined);
    _ejs_op_strict_eq(_ejs_undefined, _ejs_undefined);
    _ejs_op_strict_neq(_ejs_undefined, _ejs_undefined);
    _ejs_op_eq(_ejs_undefined, _ejs_undefined);
    _ejs_op_neq(_ejs_undefined, _ejs_undefined);
    //arguments
    _ejs_arguments_new(0, NULL);

    //Array
    _ejs_array_new(jsValue32Size, false);
    //String
    _ejs_string_new_utf8_len(NULL, 0);
    //Function
    _ejs_function_new_utf8(_ejs_undefined, NULL, (EJSClosureFunc)NULL);
    _ejs_invoke_closure(_ejs_undefined, _ejs_undefined, 0, NULL);
    //RegExp
    _ejs_regexp_new_utf8(NULL, NULL);
}