-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathjswrap_error.c
132 lines (124 loc) · 3.16 KB
/
jswrap_error.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
/*
* This file is part of Espruino, a JavaScript interpreter for Microcontrollers
*
* Copyright (C) 2013 Gordon Williams <[email protected]>
*
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
*
* ----------------------------------------------------------------------------
* This file is designed to be parsed during the build process
*
* JavaScript methods for Errors
* ----------------------------------------------------------------------------
*/
#include "jswrap_error.h"
#include "jsparse.h"
/*JSON{
"type" : "class",
"class" : "Error"
}
The base class for runtime errors
*/
/*JSON{
"type" : "class",
"class" : "SyntaxError"
}
The base class for syntax errors
*/
/*JSON{
"type" : "class",
"class" : "InternalError"
}
The base class for internal errors
*/
JsVar *_jswrap_error_constructor(JsVar *msg, char *type) {
JsVar *d = jspNewObject(0,type);
if (!d) return 0;
if (msg) {
msg = jsvAsString(msg, false);
jsvUnLock(jsvObjectSetChild(d, "msg", msg));
}
jsvUnLock(jsvObjectSetChild(d, "type", jsvNewFromString(type)));
return d;
}
/*JSON{
"type" : "constructor",
"class" : "Error",
"name" : "Error",
"generate" : "jswrap_error_constructor",
"params" : [
["message","JsVar","An optional message string"]
],
"return" : ["JsVar","An Error object"]
}
Creates an Error object
*/
JsVar *jswrap_error_constructor(JsVar *msg) {
return _jswrap_error_constructor(msg, "Error");
}
/*JSON{
"type" : "constructor",
"class" : "SyntaxError",
"name" : "SyntaxError",
"generate" : "jswrap_syntaxerror_constructor",
"params" : [
["message","JsVar","An optional message string"]
],
"return" : ["JsVar","A SyntaxError object"]
}
Creates a SyntaxError object
*/
JsVar *jswrap_syntaxerror_constructor(JsVar *msg) {
return _jswrap_error_constructor(msg, "SyntaxError");
}
/*JSON{
"type" : "constructor",
"class" : "InternalError",
"name" : "InternalError",
"generate" : "jswrap_internalerror_constructor",
"params" : [
["message","JsVar","An optional message string"]
],
"return" : ["JsVar","An InternalError object"]
}
Creates an InternalError object
*/
JsVar *jswrap_internalerror_constructor(JsVar *msg) {
return _jswrap_error_constructor(msg, "InternalError");
}
/*JSON{
"type" : "method",
"class" : "Error",
"name" : "toString",
"generate" : "jswrap_error_toString",
"return" : ["JsVar","A String"]
}*/
/*JSON{
"type" : "method",
"class" : "SyntaxError",
"name" : "toString",
"generate" : "jswrap_error_toString",
"return" : ["JsVar","A String"]
}*/
/*JSON{
"type" : "method",
"class" : "InternalError",
"name" : "toString",
"generate" : "jswrap_error_toString",
"return" : ["JsVar","A String"]
}*/
JsVar *jswrap_error_toString(JsVar *parent) {
JsVar *str = jsvObjectGetChild(parent, "type", 0);
if (!str) str = jsvNewFromString("Error");
if (!str) return 0;
JsVar *msg = jsvObjectGetChild(parent, "msg", 0);
if (msg) {
JsVar *newStr = jsvVarPrintf("%v: %v", str, msg);
jsvUnLock(msg);
jsvUnLock(str);
str = newStr;
}
return str;
}