PageRenderTime 71ms CodeModel.GetById 33ms app.highlight 33ms RepoModel.GetById 2ms app.codeStats 0ms

/vm/src/lib/error.c

https://github.com/charliesome/jsos
C | 92 lines | 78 code | 13 blank | 1 comment | 3 complexity | f3c20cfda27301d2a7633b5945be82c7 MD5 | raw file
 1#include <stddef.h>
 2#include "string.h"
 3#include "lib.h"
 4#include "exception.h"
 5
 6static VAL make_generic_error(js_string_t* name, VAL proto, VAL class, uint32_t argc, VAL* argv)
 7{
 8    VAL obj = js_value_make_object(proto, class);
 9    js_object_put(obj, js_cstring("name"), js_value_wrap_string(name));
10    if(argc > 0) {
11        js_object_put(obj, js_cstring("message"), js_to_string(argv[0]));
12    }
13    return obj;
14}
15
16static VAL Error_construct(js_vm_t* vm, void* state, VAL this, uint32_t argc, VAL* argv)
17{
18    return make_generic_error(js_cstring("Error"), vm->lib.Error_prototype, vm->lib.Error, argc, argv);
19}
20
21static VAL RangeError_construct(js_vm_t* vm, void* state, VAL this, uint32_t argc, VAL* argv)
22{
23    return make_generic_error(js_cstring("RangeError"), vm->lib.RangeError_prototype, vm->lib.RangeError, argc, argv);
24}
25
26static VAL ReferenceError_construct(js_vm_t* vm, void* state, VAL this, uint32_t argc, VAL* argv)
27{
28    return make_generic_error(js_cstring("ReferenceError"), vm->lib.ReferenceError_prototype, vm->lib.ReferenceError, argc, argv);
29}
30
31static VAL TypeError_construct(js_vm_t* vm, void* state, VAL this, uint32_t argc, VAL* argv)
32{
33    return make_generic_error(js_cstring("TypeError"), vm->lib.TypeError_prototype, vm->lib.TypeError, argc, argv);
34}
35
36static VAL Error_toString(js_vm_t* vm, void* state, VAL this, uint32_t argc, VAL* argv)
37{
38    js_string_t* message = &js_value_get_pointer(js_to_string(js_object_get(this, js_cstring("message"))))->string;
39    js_string_t* name = &js_value_get_pointer(js_to_string(js_object_get(this, js_cstring("name"))))->string;
40    js_string_t* str = js_string_concat(js_string_concat(name, js_cstring(": ")), message);
41    return js_value_wrap_string(str);
42}
43
44static VAL Error_prototype_stack(js_vm_t* vm, void* state, VAL this, uint32_t argc, VAL* argv)
45{
46    if(js_value_get_type(this) != JS_T_OBJECT) {
47        // wtf?
48        js_throw_error(vm->lib.TypeError, "can't access Error.prototype.stack on non object");
49    }
50    return js_value_wrap_string(js_value_get_pointer(this)->object.stack_trace);
51}
52
53VAL js_make_error(VAL class, js_string_t* message)
54{
55    VAL msg = js_value_wrap_string(message);
56    return js_construct(class, 1, &msg);
57}
58
59void js_throw_error(VAL class, char* fmt, ...)
60{
61    va_list va;
62    js_string_t* message;
63    va_start(va, fmt);
64    message = js_string_vformat(fmt, va);
65    va_end(va);
66    js_throw(js_make_error(class, message));
67}
68
69void js_lib_error_initialize(struct js_vm* vm)
70{
71    vm->lib.Error = js_value_make_native_function(vm, NULL, js_cstring("Error"), Error_construct, Error_construct);
72    js_object_put(vm->global_scope->global_object, js_cstring("Error"), vm->lib.Error);
73    vm->lib.Error_prototype = js_value_make_object(vm->lib.Object_prototype, vm->lib.Error);
74    js_object_put(vm->lib.Error, js_cstring("prototype"), vm->lib.Error_prototype);
75    js_object_put(vm->lib.Error_prototype, js_cstring("toString"), js_value_make_native_function(vm, js_cstring("Error"), js_cstring("toString"), Error_toString, NULL));
76    js_object_put_accessor(vm, vm->lib.Error_prototype, "stack", Error_prototype_stack, NULL);
77    
78    vm->lib.RangeError = js_value_make_native_function(vm, NULL, js_cstring("RangeError"), RangeError_construct, RangeError_construct);
79    vm->lib.RangeError_prototype = js_value_make_object(vm->lib.Error_prototype, vm->lib.Error);
80    js_object_put(vm->lib.RangeError, js_cstring("prototype"), vm->lib.RangeError_prototype);
81    js_object_put(vm->global_scope->global_object, js_cstring("RangeError"), vm->lib.RangeError);
82    
83    vm->lib.ReferenceError = js_value_make_native_function(vm, NULL, js_cstring("ReferenceError"), ReferenceError_construct, ReferenceError_construct);
84    vm->lib.ReferenceError_prototype = js_value_make_object(vm->lib.Error_prototype, vm->lib.Error);
85    js_object_put(vm->lib.ReferenceError, js_cstring("prototype"), vm->lib.ReferenceError_prototype);
86    js_object_put(vm->global_scope->global_object, js_cstring("ReferenceError"), vm->lib.ReferenceError);
87    
88    vm->lib.TypeError = js_value_make_native_function(vm, NULL, js_cstring("TypeError"), TypeError_construct, TypeError_construct);
89    vm->lib.TypeError_prototype = js_value_make_object(vm->lib.Error_prototype, vm->lib.Error);
90    js_object_put(vm->lib.TypeError, js_cstring("prototype"), vm->lib.TypeError_prototype);
91    js_object_put(vm->global_scope->global_object, js_cstring("TypeError"), vm->lib.TypeError);
92}