Example #1
0
js_string_t* js_string_vformat(char* fmt, va_list args)
{
    js_string_t* str = js_alloc(sizeof(js_string_t));
    str->buff = js_alloc_no_pointer(1024);
    str->length = vsnprintf(str->buff, 1023, fmt, args);
    str->buff[1023] = 0;
    return str;
}
Example #2
0
js_vm_t* js_vm_new()
{
    js_vm_t* vm = js_alloc(sizeof(js_vm_t));
    // this proto/constructor is fixed up later by js_lib_initialize
    vm->global_scope = js_scope_make_global(vm, js_value_make_object(js_value_undefined(), js_value_undefined()));
    js_object_put(vm->global_scope->global_object, js_cstring("global"), vm->global_scope->global_object);
    js_lib_initialize(vm);
    return vm;
}
Example #3
0
File: gc.c Project: filp/jsos
void* js_alloc_no_pointer(size_t sz)
#endif
{
    #ifdef JS_GC_DEBUG
        void* ptr = js_alloc_impl(sz, file, line);
    #else
        void* ptr = js_alloc(sz);
    #endif
    alloc_t* alloc = allocs_lookup(ptr);
    alloc->no_pointer = true;
    return ptr;    
}
Example #4
0
js_string_t* js_string_concat(js_string_t* a, js_string_t* b)
{
    if(a == NULL || b == NULL) {
        js_throw(js_value_make_cstring("js_string_concat received a NULL pointer"));
    }
    js_string_t* str = js_alloc(sizeof(js_string_t));
    str->length = a->length + b->length;
    str->buff = js_alloc_no_pointer(str->length + 1);
    memcpy(str->buff, a->buff, a->length);
    memcpy(str->buff + a->length, b->buff, b->length);
    str->buff[str->length] = 0;
    return str;
}
Example #5
0
/* A function that copies a header over; this is used so that we may echo
 * most of the bits from the header */
q_header *csv2_copy_header(q_header *in) {
        q_header *out;
        if((out = js_alloc(sizeof(q_header),1)) == 0) {
                return 0;
        }
        /* We can get away with a simple memcpy, since q_header has no
         * strings */
        if(memcpy(out,in,sizeof(q_header)) == 0) {
                js_dealloc(out);
                return 0;
        }
        return out;
}
Example #6
0
char* read_until_eof(FILE* f, uint32_t* len)
{
    size_t cap = 4096;
    size_t idx = 0;
    char* buff = js_alloc(cap);
    while(!feof(stdin)) {
        idx += fread(buff + idx, 1, 4096, f);
        if(idx >= cap) {
            cap *= 2;
            buff = js_realloc(buff, cap);
        }
    }
    *len = idx;
    return buff;
}
Example #7
0
File: gc.c Project: filp/jsos
void* js_realloc(void* ptr, size_t sz)
#endif
{
    #ifdef JS_GC_DEBUG
        void* new_ptr = js_alloc_impl(sz, file, line);
    #else
        void* new_ptr = js_alloc(sz);
    #endif
    alloc_t* alloc = allocs_lookup(ptr);
    if(alloc) {
        memcpy(new_ptr, ptr, sz > alloc->size ? alloc->size : sz);
    }
    return new_ptr;
    // @TODO attempt to realloc existing block
    /*
    alloc_t* alloc = allocs_lookup(ptr);
    void* new_ptr;
    uint16_t new_hash;
    if(alloc == NULL) {
        return js_alloc(sz);
    }
    new_ptr = realloc(ptr, sz);
    if(ptr == new_ptr) {
        return ptr;
    }
    alloc->ptr = new_ptr;
    alloc->size = sz;
    if(alloc->prev) {
        alloc->prev->next = alloc->next;
    }
    if(alloc->next) {
        alloc->next->prev = alloc->prev;
    }
    new_hash = pointer_hash(alloc->ptr);
    alloc->prev = NULL;
    alloc->next = allocs[new_hash];
    if(alloc->next) {
        alloc->next->prev = alloc;
    }
    allocs[new_hash] = alloc;
    return new_ptr;
    */
}
Example #8
0
void* js_realloc(void* ptr, size_t sz)
#endif
{
    alloc_t* alloc = allocs_lookup(ptr);
    void* new_ptr;
    uint16_t new_hash;
    if(alloc == NULL) {
        #ifdef JS_GC_DEBUG
            return js_alloc_impl(sz, file, line);
        #else
            return js_alloc(sz);
        #endif
    }
    new_ptr = realloc(ptr, sz);
    if(new_ptr == ptr) {
        alloc->size = sz;
        return new_ptr;
    }
    
    // pointer has changed, so unlink this alloc from its bucket:    
    new_hash = pointer_hash(new_ptr);
    if(alloc->prev == NULL) {
        allocs[pointer_hash(ptr)] = alloc->next;
    } else {
        alloc->prev->next = alloc->next;
    }
    if(alloc->next) {
        alloc->next->prev = alloc->prev;
    }
    // change the pointer and size:
    alloc->ptr = new_ptr;
    alloc->size = sz;
    // add to the start of its new bucket:
    alloc->next = allocs[new_hash];
    if(alloc->next) {
        alloc->next->prev = alloc;
    }
    alloc->prev = NULL;
    allocs[new_hash] = alloc;
    // and return the pointer to the new allocation:
    return new_ptr;
}
Example #9
0
File: object.c Project: filp/jsos
static void js_object_base_put(js_value_t* obj, js_string_t* prop, VAL value)
{
    js_property_descriptor_t* descr = NULL;
    if(st_lookup(obj->object.properties, (st_data_t)prop, (st_data_t*)&descr)) {
        if(!descr->is_accessor) {
            if(descr->data.writable) {
                descr->data.value = value;
            }
        } else {
            if(js_value_get_type(descr->accessor.set) == JS_T_FUNCTION) {
                js_call(descr->accessor.set, js_value_make_pointer(obj), 1, &value);
            }
        }
        return;
    }
    descr = js_alloc(sizeof(js_property_descriptor_t));
    descr->is_accessor = false;
    descr->enumerable = true;
    descr->configurable = true;
    descr->data.value = value;
    descr->data.writable = true;
    st_insert(obj->object.properties, (st_data_t)prop, (st_data_t)descr);
}
Example #10
0
int main() {
    js_string *s1,*s2,*s3,*get;
    js_file *f1;
    char strn[256];
    int counter,place,number;

    s1=js_create(256,1);
    s2=js_create(256,1);
    s2=js_create(256,1);

    /* Test reading a file line by line */
    js_qstr2js(s1,"8859-1data");
    f1 = js_alloc(1,sizeof(js_file));
    js_open_read(s1,f1);
    js_set_encode(s1,JS_8859_1); /* Mandatory for line-by-line reading */
    while(!js_buf_eof(f1)) {
        printf("%s","Reading a line from file:\n");
        js_buf_getline(f1,s1);
        iso88591_to_utf8(s1,s2);
        js_show_stdout(s1);
        js_show_stdout(s2);
        }

    }
Example #11
0
int main() {
    js_string *s1,*s2,*s3,*get;
    js_file *f1;
    mhash *dict;
    char c;
    char strn[256];
    int counter,place,number;

    /* Test creation of the string */
    s1 = js_create(256,1);
    printf("%s\n","Test: String creation");
    printf("%s%p\n","The following number should be a valid address: ",s1);

    printf("\n");

    /* Put the string 'What's up doc?' in the js_string object */
    /* Test conversion of classicsal C null-terminated string to
       js_ String object */

    printf("%s\n","Test: String conversion from null-terminated string");
    js_str2js(s1,"What's up doc?",14,1);
    printf("%s\n",
           "The following line should have the string \"What's up doc?\"");
    js_show_stdout(s1);
    printf("\n");

    printf("\n");

    /* Test appending to a string */
    printf("%s\n","Test: Appending one string to another");
    s2 = js_create(256,1);
    js_str2js(s2," is what the bunny said.",24,1);
    /* Append the contents of s2 to s1 */
    js_append(s2,s1);
    printf("%s\n","You should see \"What's up doc? is what the bunny said.\"");
    js_show_stdout(s1);

    /* test the hash function */
    printf("\n");
    printf("%s","Test: the MaraHash library\n");
    for(counter=8;counter<24;counter++)
        printf("s1 Hash: %d\n",mhash_js(s1,counter));
    for(counter=8;counter<24;counter++)
        printf("s2 Hash: %d\n",mhash_js(s2,counter));
    printf("\n");

    /* Test the ability to put and get info from a dictionary */
    dict = (mhash *)mhash_create(8);
    if(dict == 0) {
        printf("Fatal: Couldn't make Dictionary!\n");
        exit(1);
        }
    /* Add some elements to the dictionary */
    js_qstr2js(s1,"key");
    js_qstr2js(s2,"value");
    mhash_put_js(dict,s1,s2);
    js_qstr2js(s1,"otherkey");
    js_qstr2js(s2,"hihihi");
    mhash_put_js(dict,s1,s2);
    get = mhash_get_js(dict,s1);
    printf("%s\n","You should see:\nhihihi\nvalue\n\n");
    js_show_stdout(get);
    printf("\n");
    js_qstr2js(s1,"key");
    get = mhash_get_js(dict,s1);
    js_show_stdout(get);
    printf("\n");

    /* Some more hash testing */
    printf("Interactive session: add/remove/view mhash elements\n");
    printf("key = value, otherkey = hihihi, otherwise empty mhash\n");
    for(;;) {
        char *nowarn;
        printf("a to add, v to view, d to delete, and q to quit\n");
        printf("Enter command: ");
        nowarn = fgets(strn,200,stdin);
        if(strn[strlen(strn) - 1] == '\n')
            strn[strlen(strn) - 1] = '\0';
        if(*strn == 'a') {
            printf("Element to add: ");
            nowarn = fgets(strn,200,stdin);
            if(strn[strlen(strn) - 1] == '\n')
                strn[strlen(strn) - 1] = '\0';
            js_qstr2js(s1,strn);
            printf("Value of element: ");
            nowarn = fgets(strn,200,stdin);
            if(strn[strlen(strn) - 1] == '\n')
                strn[strlen(strn) - 1] = '\0';
            js_qstr2js(s2,strn);
            printf("mhash_put_js returned %d\n",mhash_put_js(dict,s1,s2));
            }
        else if(*strn == 'v') {
            printf("Element to view: ");
            nowarn = fgets(strn,200,stdin);
            if(strn[strlen(strn) - 1] == '\n')
                strn[strlen(strn) - 1] = '\0';
            js_qstr2js(s1,strn);
            get = mhash_get_js(dict,s1);
            printf("mhash_get_js returned %p\n",get);
            printf("Viewing element %s: ",strn);
            js_show_stdout(get);
            printf("\n");
            }
        else if(*strn == 'd') {
            printf("Element to delete: ");
            nowarn = fgets(strn,200,stdin);
            if(strn[strlen(strn) - 1] == '\n')
                strn[strlen(strn) - 1] = '\0';
            js_qstr2js(s1,strn);
            printf("mhash_undef_js returned %d\n",mhash_undef_js(dict,s1));
            }
        else if(*strn == 'q')
            break;
        }
    printf("Continuing with ks_String tests.....\n");

    /* Test qappend function -- fast string appending */
    printf("%s\n","Test: js_qappend and js_qstr2js functions");
    js_qstr2js(s1,"What's up doc");
    printf("%s\n","You should see \"What's up doc is what the bunny said\"");
    js_qappend(" is what the bunny said",s1);
    js_show_stdout(s1);
    printf("\n");

    printf("\n");

    /* Test qprepend function -- fast string prepending */
    printf("%s\n","Test: js_qprepend and js_qstr2js functions");
    js_qstr2js(s1," what's up doc");
    printf("%s\n","You should see \"is what the bunny said what's up doc\"");
    js_qprepend("is what the bunny said",s1);
    js_show_stdout(s1);
    printf("\n");

    printf("\n");

    /* Test qfgrep function -- fast string fgrepping */
    printf("%s\n","Test: js_qfgrep function");
    printf("%s\n","You should see 12");
    printf("%d\n",js_qfgrep("bunny",s1));
    printf("\n");

    printf("\n");

    /* Test inserting in a string */
    printf("%s\n","Test: Inserting one string inside another");
    for(counter=0;counter<=10;counter++) {
        js_destroy(s1);
        js_destroy(s2);
        s1 = js_create(256,1);
        s2 = js_create(256,1);
        js_str2js(s1,"1234567890",10,1);
        js_str2js(s2,"insert",6,1);
        js_insert(s2,s1,counter);
        printf("%s","You should see \"");
        place = 1;
        while(place <= counter) {
            printf("%d",place % 10);
            place++;
            }
        printf("%s","insert");
        while(place <= 10) {
            printf("%d",place % 10);
            place++;
            }
        printf("%s","\"\n");
        js_show_stdout(s1);
        printf("%s","\n");
        }

    printf("%s","\n");

    /* Test fgreping a string */
    printf("%s\n","Test: Fgrepping one string inside another");
    printf("%s\n","You should see \"0 1 2 3 4 5 6 7 8 9 10 \"");
    for(counter=0;counter<=10;counter++) {
        js_destroy(s1);
        js_destroy(s2);
        s1 = js_create(256,1);
        s2 = js_create(256,1);
        js_str2js(s1,"1234567890",10,1);
        js_str2js(s2,"insert",6,1);
        js_insert(s2,s1,counter);
        printf("%d ",js_fgrep(s2,s1));
        }

    printf("%s","\n");
    printf("%s","\n");

    /* Test fgreping a string with offset */
    printf("%s\n","Test: Fgrepping one string inside another with offset");
    for(counter=1;counter<=10;counter++) {
        printf("%s","You should see \"");
        for(place=0;place<counter;place++)
            printf("%s","-2 ");
        for(;place<=10;place++)
            printf("%d ",place);
        printf("%s","\"\n");
        for(place=0;place<=10;place++) {
            js_destroy(s1);
            js_destroy(s2);
            s1 = js_create(256,1);
            s2 = js_create(256,1);
            js_str2js(s1,"1234567890",10,1);
            js_str2js(s2,"insert",6,1);
            js_insert(s2,s1,place);
            printf("%d ",js_fgrep_offset(s2,s1,counter));
            }
        printf("%s","\n");
        }

    printf("%s","\n");

    /* Test matching one string inside another */
    printf("%s\n","Test: Matching one string against another");
    printf("%s\n","You should see \"0 1 2 3 4 5 6 7 8 9 \"");
    js_destroy(s1);
    js_destroy(s2);
    s1 = js_create(256,1);
    s2 = js_create(256,1);
    js_str2js(s1,"1234567890",10,1);
    for(counter=1;counter<=10;counter++) {
        c = '0' + counter % 10;
        js_str2js(s2,&c,1,1);
        printf("%d ",js_match(s2,s1));
        }
    printf("%s","\n");

    printf("%s","\n");

    /* Test matching one string inside another with offset */
    printf("%s\n","Test: Matching one string against another with offset");
    for(place=1;place<10;place++) {
        printf("%s","You should see \"");
        for(counter=0;counter<place;counter++)
            printf("%s","-2 ");
        for(;counter<10;counter++)
            printf("%d ",counter);
        printf("%s","\"\n");
        js_destroy(s1);
        js_destroy(s2);
        s1 = js_create(256,1);
        s2 = js_create(256,1);
        js_str2js(s1,"1234567890",10,1);
        for(counter=1;counter<=10;counter++) {
            c = '0' + counter % 10;
            js_str2js(s2,&c,1,1);
            printf("%d ",js_match_offset(s2,s1,place));
            }
        printf("%s","\n");
        }

    printf("%s","\n");

    /* Test matching one string inside another (multichar expression)*/
    printf("%s\n","Test: Matching one string against another (multichar expression)");
    for(place=1;place<=10;place++) {
        printf("%s","You should see \"");
        for(counter=0;counter<10;counter++)
            printf("%d ",place - 1);
        printf("%s","\"\n");
        s3 = js_create(1,1);
        for(number=1;number<=10;number++) {
            js_destroy(s1);
            js_destroy(s2);
            js_destroy(s3);
            s1 = js_create(256,1);
            s2 = js_create(256,1);
            s3 = js_create(256,1);
            js_str2js(s1,"1234567890",10,1);
            for(counter=1;counter<=10;counter++) {
                if(counter==number)
                    c = '0' + place % 10;
                else
                    c = 'a' + counter;
                js_str2js(s3,&c,1,1);
                js_append(s3,s2);
                }
            printf("%d ",js_match(s2,s1));
            }
        printf("%s","\n");
        }

    printf("%s","\n");

    /* Test matching one string inside another (multichar expression 2) */
    printf("%s\n","Test: Matching one string against another (multichar expression 2)");
    for(place=1;place<=10;place++) {
        printf("%s","You should see \"");
        for(counter=0;counter<10;counter++)
            printf("%d ",counter);
        printf("%s","\"\n");
        for(number=1;number<=10;number++) {
            js_destroy(s1);
            js_destroy(s2);
            js_destroy(s3);
            s1 = js_create(256,1);
            s2 = js_create(256,1);
            s3 = js_create(256,1);
            js_str2js(s1,"1234567890",10,1);
            for(counter=1;counter<=10;counter++) {
                if(counter==number)
                    c = '0' + place % 10;
                else
                    c = 'a' + counter;
                js_str2js(s3,&c,1,1);
                js_append(s3,s2);
                }
            printf("%d ",js_match(s1,s2));
            }
        printf("%s","\n");
        }

    printf("%s","\n");

    /* Test match with multioctet chars */
    printf("%s\n","Test: Match with multioctet chars");
    js_destroy(s1);
    js_destroy(s2);
    js_destroy(s3);
    s1 = js_create(128,2);
    s2 = js_create(128,2);
    js_str2js(s1,"timokutokita",6,2);
    js_str2js(s2,"mmkkkkkikktt",6,2);
    printf("%s","You should see 3\n");
    printf("%d\n",js_match(s1,s2));
    js_str2js(s2,"mmkukkkxkktt",6,2);
    printf("%s","You should see 1\n");
    printf("%d\n",js_match(s1,s2));
    js_str2js(s2,"mokukkkxkktt",6,2);
    printf("%s","You should see 0\n");
    printf("%d\n",js_match(s1,s2));
    js_str2js(s2,"zxkzkukukuto",6,2);
    printf("%s","You should see 2\n");
    printf("%d\n",js_match(s1,s2));
    js_str2js(s2,"xxkzkqkxkztb",6,2);
    printf("%s","You should see -2\n");
    printf("%d\n",js_match(s1,s2));

    js_destroy(s1);
    js_destroy(s2);
    printf("%s","\n");

    /* Test js_substr function */
    s1 = js_create(256,1);
    s2 = js_create(256,1);
    js_str2js(s1,"1234567890",10,1);
    js_substr(s1,s2,2,3);
    printf("%s","You should see \"345\"\n");
    js_show_stdout(s2);
    printf("%s","\n");

    /* Test "not"matching one string inside another */
    printf("%s\n","Test: NotMatching one string against another");
    printf("%s\n","You should see \"1 2 3 4 5 6 7 8 9 -2\"");
    js_destroy(s1);
    js_destroy(s2);
    s1 = js_create(256,1);
    s2 = js_create(256,1);
    s3 = js_create(256,1);
    js_str2js(s1,"1234567890",10,1);
    for(counter=1;counter<=10;counter++) {
        c = '0' + counter % 10;
        js_str2js(s2,&c,1,1);
        js_append(s2,s3);
        printf("%d ",js_notmatch(s3,s1));
        }
    printf("%s","\n");

    printf("%s","\n");

    /* Test reading a file line by line */
    js_str2js(s1,"testdata",8,1);
    f1 = js_alloc(1,sizeof(js_file));
    js_open_read(s1,f1);
    js_set_encode(s1,JS_US_ASCII); /* Mandatory for line-by-line reading */
    while(!js_buf_eof(f1)) {
        printf("%s","Reading a line from file:\n");
        js_buf_getline(f1,s1);
        js_show_stdout(s1);
        }

    /* Test js_atoi */
    js_qstr2js(s1,"12345");
    printf("%s","\nTest: js_atoi\n");
    printf("%s","You should see: 12345 2345 345 45 5 0\n");
    for(counter=0; counter<=5; counter++)
        printf("%d ",js_atoi(s1,counter));
    printf("\n");

    /* Test js_val */
    printf("%s","\nTest: js_val\n");
    printf("%s","You should see: 49 50 51 52 53\n");
    for(counter=0;counter<5;counter++)
        printf("%d ",js_val(s1,counter));
    printf("\n");

    return 0;
    }