PageRenderTime 76ms CodeModel.GetById 11ms RepoModel.GetById 1ms 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. static VAL make_generic_error(js_string_t* name, VAL proto, VAL class, uint32_t argc, VAL* argv)
  6. {
  7. VAL obj = js_value_make_object(proto, class);
  8. js_object_put(obj, js_cstring("name"), js_value_wrap_string(name));
  9. if(argc > 0) {
  10. js_object_put(obj, js_cstring("message"), js_to_string(argv[0]));
  11. }
  12. return obj;
  13. }
  14. static VAL Error_construct(js_vm_t* vm, void* state, VAL this, uint32_t argc, VAL* argv)
  15. {
  16. return make_generic_error(js_cstring("Error"), vm->lib.Error_prototype, vm->lib.Error, argc, argv);
  17. }
  18. static VAL RangeError_construct(js_vm_t* vm, void* state, VAL this, uint32_t argc, VAL* argv)
  19. {
  20. return make_generic_error(js_cstring("RangeError"), vm->lib.RangeError_prototype, vm->lib.RangeError, argc, argv);
  21. }
  22. static VAL ReferenceError_construct(js_vm_t* vm, void* state, VAL this, uint32_t argc, VAL* argv)
  23. {
  24. return make_generic_error(js_cstring("ReferenceError"), vm->lib.ReferenceError_prototype, vm->lib.ReferenceError, argc, argv);
  25. }
  26. static VAL TypeError_construct(js_vm_t* vm, void* state, VAL this, uint32_t argc, VAL* argv)
  27. {
  28. return make_generic_error(js_cstring("TypeError"), vm->lib.TypeError_prototype, vm->lib.TypeError, argc, argv);
  29. }
  30. static VAL Error_toString(js_vm_t* vm, void* state, VAL this, uint32_t argc, VAL* argv)
  31. {
  32. js_string_t* message = &js_value_get_pointer(js_to_string(js_object_get(this, js_cstring("message"))))->string;
  33. js_string_t* name = &js_value_get_pointer(js_to_string(js_object_get(this, js_cstring("name"))))->string;
  34. js_string_t* str = js_string_concat(js_string_concat(name, js_cstring(": ")), message);
  35. return js_value_wrap_string(str);
  36. }
  37. static VAL Error_prototype_stack(js_vm_t* vm, void* state, VAL this, uint32_t argc, VAL* argv)
  38. {
  39. if(js_value_get_type(this) != JS_T_OBJECT) {
  40. // wtf?
  41. js_throw_error(vm->lib.TypeError, "can't access Error.prototype.stack on non object");
  42. }
  43. return js_value_wrap_string(js_value_get_pointer(this)->object.stack_trace);
  44. }
  45. VAL js_make_error(VAL class, js_string_t* message)
  46. {
  47. VAL msg = js_value_wrap_string(message);
  48. return js_construct(class, 1, &msg);
  49. }
  50. void js_throw_error(VAL class, char* fmt, ...)
  51. {
  52. va_list va;
  53. js_string_t* message;
  54. va_start(va, fmt);
  55. message = js_string_vformat(fmt, va);
  56. va_end(va);
  57. js_throw(js_make_error(class, message));
  58. }
  59. void js_lib_error_initialize(struct js_vm* vm)
  60. {
  61. vm->lib.Error = js_value_make_native_function(vm, NULL, js_cstring("Error"), Error_construct, Error_construct);
  62. js_object_put(vm->global_scope->global_object, js_cstring("Error"), vm->lib.Error);
  63. vm->lib.Error_prototype = js_value_make_object(vm->lib.Object_prototype, vm->lib.Error);
  64. js_object_put(vm->lib.Error, js_cstring("prototype"), vm->lib.Error_prototype);
  65. 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));
  66. js_object_put_accessor(vm, vm->lib.Error_prototype, "stack", Error_prototype_stack, NULL);
  67. vm->lib.RangeError = js_value_make_native_function(vm, NULL, js_cstring("RangeError"), RangeError_construct, RangeError_construct);
  68. vm->lib.RangeError_prototype = js_value_make_object(vm->lib.Error_prototype, vm->lib.Error);
  69. js_object_put(vm->lib.RangeError, js_cstring("prototype"), vm->lib.RangeError_prototype);
  70. js_object_put(vm->global_scope->global_object, js_cstring("RangeError"), vm->lib.RangeError);
  71. vm->lib.ReferenceError = js_value_make_native_function(vm, NULL, js_cstring("ReferenceError"), ReferenceError_construct, ReferenceError_construct);
  72. vm->lib.ReferenceError_prototype = js_value_make_object(vm->lib.Error_prototype, vm->lib.Error);
  73. js_object_put(vm->lib.ReferenceError, js_cstring("prototype"), vm->lib.ReferenceError_prototype);
  74. js_object_put(vm->global_scope->global_object, js_cstring("ReferenceError"), vm->lib.ReferenceError);
  75. vm->lib.TypeError = js_value_make_native_function(vm, NULL, js_cstring("TypeError"), TypeError_construct, TypeError_construct);
  76. vm->lib.TypeError_prototype = js_value_make_object(vm->lib.Error_prototype, vm->lib.Error);
  77. js_object_put(vm->lib.TypeError, js_cstring("prototype"), vm->lib.TypeError_prototype);
  78. js_object_put(vm->global_scope->global_object, js_cstring("TypeError"), vm->lib.TypeError);
  79. }