1 |
/* -*- Mode: C; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 4 -*- |
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 4 -*- |
2 |
* vim: set ts=8 sw=4 et tw=78: |
* vim: set ts=8 sw=4 et tw=78: |
3 |
* |
* |
4 |
* ***** BEGIN LICENSE BLOCK ***** |
* ***** BEGIN LICENSE BLOCK ***** |
42 |
* JS standard exception implementation. |
* JS standard exception implementation. |
43 |
*/ |
*/ |
44 |
|
|
|
#include "jsstddef.h" |
|
45 |
#include <stdlib.h> |
#include <stdlib.h> |
46 |
#include <string.h> |
#include <string.h> |
47 |
#include "jstypes.h" |
#include "jstypes.h" |
48 |
|
#include "jsstdint.h" |
49 |
#include "jsbit.h" |
#include "jsbit.h" |
50 |
#include "jsutil.h" /* Added by JSIFY */ |
#include "jsutil.h" /* Added by JSIFY */ |
51 |
#include "jsprf.h" |
#include "jsprf.h" |
164 |
*/ |
*/ |
165 |
mallocSize = sizeof(JSErrorReport) + argsArraySize + argsCopySize + |
mallocSize = sizeof(JSErrorReport) + argsArraySize + argsCopySize + |
166 |
ucmessageSize + uclinebufSize + linebufSize + filenameSize; |
ucmessageSize + uclinebufSize + linebufSize + filenameSize; |
167 |
cursor = (uint8 *)JS_malloc(cx, mallocSize); |
cursor = (uint8 *)cx->malloc(mallocSize); |
168 |
if (!cursor) |
if (!cursor) |
169 |
return NULL; |
return NULL; |
170 |
|
|
281 |
if (fp->fun && fp->argv) { |
if (fp->fun && fp->argv) { |
282 |
v = JSVAL_NULL; |
v = JSVAL_NULL; |
283 |
if (checkAccess && |
if (checkAccess && |
284 |
!checkAccess(cx, fp->callee, callerid, JSACC_READ, &v)) { |
!checkAccess(cx, fp->callee(), callerid, JSACC_READ, &v)) { |
285 |
break; |
break; |
286 |
} |
} |
287 |
valueCount += fp->argc; |
valueCount += fp->argc; |
301 |
js_ReportAllocationOverflow(cx); |
js_ReportAllocationOverflow(cx); |
302 |
return JS_FALSE; |
return JS_FALSE; |
303 |
} |
} |
304 |
priv = (JSExnPrivate *)JS_malloc(cx, size); |
priv = (JSExnPrivate *)cx->malloc(size); |
305 |
if (!priv) |
if (!priv) |
306 |
return JS_FALSE; |
return JS_FALSE; |
307 |
|
|
342 |
JS_ASSERT(priv->stackElems + stackDepth == elem); |
JS_ASSERT(priv->stackElems + stackDepth == elem); |
343 |
JS_ASSERT(GetStackTraceValueBuffer(priv) + valueCount == values); |
JS_ASSERT(GetStackTraceValueBuffer(priv) + valueCount == values); |
344 |
|
|
345 |
STOBJ_SET_SLOT(exnObject, JSSLOT_PRIVATE, PRIVATE_TO_JSVAL(priv)); |
exnObject->setPrivate(priv); |
346 |
|
|
347 |
if (report) { |
if (report) { |
348 |
/* |
/* |
361 |
return JS_TRUE; |
return JS_TRUE; |
362 |
} |
} |
363 |
|
|
364 |
static JSExnPrivate * |
static inline JSExnPrivate * |
365 |
GetExnPrivate(JSContext *cx, JSObject *obj) |
GetExnPrivate(JSContext *cx, JSObject *obj) |
366 |
{ |
{ |
367 |
jsval privateValue; |
return (JSExnPrivate *) obj->getPrivate(); |
|
JSExnPrivate *priv; |
|
|
|
|
|
JS_ASSERT(OBJ_GET_CLASS(cx, obj) == &js_ErrorClass); |
|
|
privateValue = OBJ_GET_SLOT(cx, obj, JSSLOT_PRIVATE); |
|
|
if (JSVAL_IS_VOID(privateValue)) |
|
|
return NULL; |
|
|
priv = (JSExnPrivate *)JSVAL_TO_PRIVATE(privateValue); |
|
|
JS_ASSERT(priv); |
|
|
return priv; |
|
368 |
} |
} |
369 |
|
|
370 |
static void |
static void |
408 |
priv = GetExnPrivate(cx, obj); |
priv = GetExnPrivate(cx, obj); |
409 |
if (priv) { |
if (priv) { |
410 |
if (priv->errorReport) |
if (priv->errorReport) |
411 |
JS_free(cx, priv->errorReport); |
cx->free(priv->errorReport); |
412 |
JS_free(cx, priv); |
cx->free(priv); |
413 |
} |
} |
414 |
} |
} |
415 |
|
|
436 |
if (!js_LookupProperty(cx, obj, ATOM_TO_JSID(atom), &pobj, &prop)) |
if (!js_LookupProperty(cx, obj, ATOM_TO_JSID(atom), &pobj, &prop)) |
437 |
return JS_FALSE; |
return JS_FALSE; |
438 |
if (prop) |
if (prop) |
439 |
OBJ_DROP_PROPERTY(cx, pobj, prop); |
pobj->dropProperty(cx, prop); |
440 |
} |
} |
441 |
return JS_TRUE; |
return JS_TRUE; |
442 |
} |
} |
577 |
if (stackmax >= STACK_LENGTH_LIMIT) \ |
if (stackmax >= STACK_LENGTH_LIMIT) \ |
578 |
goto done; \ |
goto done; \ |
579 |
stackmax = stackmax ? 2 * stackmax : 64; \ |
stackmax = stackmax ? 2 * stackmax : 64; \ |
580 |
ptr_ = JS_realloc(cx, stackbuf, (stackmax+1) * sizeof(jschar)); \ |
ptr_ = cx->realloc(stackbuf, (stackmax+1) * sizeof(jschar)); \ |
581 |
if (!ptr_) \ |
if (!ptr_) \ |
582 |
goto bad; \ |
goto bad; \ |
583 |
stackbuf = (jschar *) ptr_; \ |
stackbuf = (jschar *) ptr_; \ |
588 |
#define APPEND_STRING_TO_STACK(str) \ |
#define APPEND_STRING_TO_STACK(str) \ |
589 |
JS_BEGIN_MACRO \ |
JS_BEGIN_MACRO \ |
590 |
JSString *str_ = str; \ |
JSString *str_ = str; \ |
591 |
jschar *chars_; \ |
const jschar *chars_; \ |
592 |
size_t length_; \ |
size_t length_; \ |
593 |
\ |
\ |
594 |
JSSTRING_CHARS_AND_LENGTH(str_, chars_, length_); \ |
str_->getCharsAndLength(chars_, length_); \ |
595 |
if (length_ > stackmax - stacklen) { \ |
if (length_ > stackmax - stacklen) { \ |
596 |
void *ptr_; \ |
void *ptr_; \ |
597 |
if (stackmax >= STACK_LENGTH_LIMIT || \ |
if (stackmax >= STACK_LENGTH_LIMIT || \ |
599 |
goto done; \ |
goto done; \ |
600 |
} \ |
} \ |
601 |
stackmax = JS_BIT(JS_CeilingLog2(stacklen + length_)); \ |
stackmax = JS_BIT(JS_CeilingLog2(stacklen + length_)); \ |
602 |
ptr_ = JS_realloc(cx, stackbuf, (stackmax+1) * sizeof(jschar)); \ |
ptr_ = cx->realloc(stackbuf, (stackmax+1) * sizeof(jschar)); \ |
603 |
if (!ptr_) \ |
if (!ptr_) \ |
604 |
goto bad; \ |
goto bad; \ |
605 |
stackbuf = (jschar *) ptr_; \ |
stackbuf = (jschar *) ptr_; \ |
650 |
* don't use JS_realloc here; simply let the oversized allocation |
* don't use JS_realloc here; simply let the oversized allocation |
651 |
* be owned by the string in that rare case. |
* be owned by the string in that rare case. |
652 |
*/ |
*/ |
653 |
void *shrunk = JS_realloc(cx, stackbuf, (stacklen+1) * sizeof(jschar)); |
void *shrunk = cx->realloc(stackbuf, (stacklen+1) * sizeof(jschar)); |
654 |
if (shrunk) |
if (shrunk) |
655 |
stackbuf = (jschar *) shrunk; |
stackbuf = (jschar *) shrunk; |
656 |
} |
} |
662 |
|
|
663 |
bad: |
bad: |
664 |
if (stackbuf) |
if (stackbuf) |
665 |
JS_free(cx, stackbuf); |
cx->free(stackbuf); |
666 |
return NULL; |
return NULL; |
667 |
} |
} |
668 |
|
|
695 |
* js_NewObject to find the class prototype, we must get the class |
* js_NewObject to find the class prototype, we must get the class |
696 |
* prototype ourselves. |
* prototype ourselves. |
697 |
*/ |
*/ |
698 |
if (!OBJ_GET_PROPERTY(cx, JSVAL_TO_OBJECT(argv[-2]), |
if (!JSVAL_TO_OBJECT(argv[-2])->getProperty(cx, |
699 |
ATOM_TO_JSID(cx->runtime->atomState |
ATOM_TO_JSID(cx->runtime->atomState |
700 |
.classPrototypeAtom), |
.classPrototypeAtom), |
701 |
rval)) |
rval)) { |
702 |
return JS_FALSE; |
return JS_FALSE; |
703 |
obj = js_NewObject(cx, &js_ErrorClass, JSVAL_TO_OBJECT(*rval), NULL, 0); |
} |
704 |
|
obj = js_NewObject(cx, &js_ErrorClass, JSVAL_TO_OBJECT(*rval), NULL); |
705 |
if (!obj) |
if (!obj) |
706 |
return JS_FALSE; |
return JS_FALSE; |
707 |
*rval = OBJECT_TO_JSVAL(obj); |
*rval = OBJECT_TO_JSVAL(obj); |
712 |
* data so that the finalizer doesn't attempt to free it. |
* data so that the finalizer doesn't attempt to free it. |
713 |
*/ |
*/ |
714 |
if (OBJ_GET_CLASS(cx, obj) == &js_ErrorClass) |
if (OBJ_GET_CLASS(cx, obj) == &js_ErrorClass) |
715 |
STOBJ_SET_SLOT(obj, JSSLOT_PRIVATE, JSVAL_VOID); |
obj->setPrivate(NULL); |
716 |
|
|
717 |
/* Set the 'message' property. */ |
/* Set the 'message' property. */ |
718 |
if (argc != 0) { |
if (argc != 0) { |
774 |
size_t name_length, message_length, length; |
size_t name_length, message_length, length; |
775 |
|
|
776 |
obj = JS_THIS_OBJECT(cx, vp); |
obj = JS_THIS_OBJECT(cx, vp); |
777 |
if (!obj || |
if (!obj || !obj->getProperty(cx, ATOM_TO_JSID(cx->runtime->atomState.nameAtom), &v)) |
|
!OBJ_GET_PROPERTY(cx, obj, |
|
|
ATOM_TO_JSID(cx->runtime->atomState.nameAtom), |
|
|
&v)) { |
|
778 |
return JS_FALSE; |
return JS_FALSE; |
|
} |
|
779 |
name = JSVAL_IS_STRING(v) ? JSVAL_TO_STRING(v) : cx->runtime->emptyString; |
name = JSVAL_IS_STRING(v) ? JSVAL_TO_STRING(v) : cx->runtime->emptyString; |
780 |
*vp = STRING_TO_JSVAL(name); |
*vp = STRING_TO_JSVAL(name); |
781 |
|
|
784 |
message = JSVAL_IS_STRING(v) ? JSVAL_TO_STRING(v) |
message = JSVAL_IS_STRING(v) ? JSVAL_TO_STRING(v) |
785 |
: cx->runtime->emptyString; |
: cx->runtime->emptyString; |
786 |
|
|
787 |
if (JSSTRING_LENGTH(message) != 0) { |
if (message->length() != 0) { |
788 |
name_length = JSSTRING_LENGTH(name); |
name_length = name->length(); |
789 |
message_length = JSSTRING_LENGTH(message); |
message_length = message->length(); |
790 |
length = (name_length ? name_length + 2 : 0) + message_length; |
length = (name_length ? name_length + 2 : 0) + message_length; |
791 |
cp = chars = (jschar *) JS_malloc(cx, (length + 1) * sizeof(jschar)); |
cp = chars = (jschar *) cx->malloc((length + 1) * sizeof(jschar)); |
792 |
if (!chars) |
if (!chars) |
793 |
return JS_FALSE; |
return JS_FALSE; |
794 |
|
|
795 |
if (name_length) { |
if (name_length) { |
796 |
js_strncpy(cp, JSSTRING_CHARS(name), name_length); |
js_strncpy(cp, name->chars(), name_length); |
797 |
cp += name_length; |
cp += name_length; |
798 |
*cp++ = ':'; *cp++ = ' '; |
*cp++ = ':'; *cp++ = ' '; |
799 |
} |
} |
800 |
js_strncpy(cp, JSSTRING_CHARS(message), message_length); |
js_strncpy(cp, message->chars(), message_length); |
801 |
cp += message_length; |
cp += message_length; |
802 |
*cp = 0; |
*cp = 0; |
803 |
|
|
804 |
result = js_NewString(cx, chars, length); |
result = js_NewString(cx, chars, length); |
805 |
if (!result) { |
if (!result) { |
806 |
JS_free(cx, chars); |
cx->free(chars); |
807 |
return JS_FALSE; |
return JS_FALSE; |
808 |
} |
} |
809 |
} else { |
} else { |
831 |
jschar *chars, *cp; |
jschar *chars, *cp; |
832 |
|
|
833 |
obj = JS_THIS_OBJECT(cx, vp); |
obj = JS_THIS_OBJECT(cx, vp); |
834 |
if (!obj || |
if (!obj || !obj->getProperty(cx, ATOM_TO_JSID(cx->runtime->atomState.nameAtom), vp)) |
|
!OBJ_GET_PROPERTY(cx, obj, |
|
|
ATOM_TO_JSID(cx->runtime->atomState.nameAtom), |
|
|
vp)) { |
|
835 |
return JS_FALSE; |
return JS_FALSE; |
|
} |
|
836 |
name = js_ValueToString(cx, *vp); |
name = js_ValueToString(cx, *vp); |
837 |
if (!name) |
if (!name) |
838 |
return JS_FALSE; |
return JS_FALSE; |
870 |
ok = JS_FALSE; |
ok = JS_FALSE; |
871 |
goto out; |
goto out; |
872 |
} |
} |
873 |
lineno_length = JSSTRING_LENGTH(lineno_as_str); |
lineno_length = lineno_as_str->length(); |
874 |
} else { |
} else { |
875 |
lineno_as_str = NULL; |
lineno_as_str = NULL; |
876 |
lineno_length = 0; |
lineno_length = 0; |
877 |
} |
} |
878 |
|
|
879 |
/* Magic 8, for the characters in ``(new ())''. */ |
/* Magic 8, for the characters in ``(new ())''. */ |
880 |
name_length = JSSTRING_LENGTH(name); |
name_length = name->length(); |
881 |
message_length = JSSTRING_LENGTH(message); |
message_length = message->length(); |
882 |
length = 8 + name_length + message_length; |
length = 8 + name_length + message_length; |
883 |
|
|
884 |
filename_length = JSSTRING_LENGTH(filename); |
filename_length = filename->length(); |
885 |
if (filename_length != 0) { |
if (filename_length != 0) { |
886 |
/* append filename as ``, {filename}'' */ |
/* append filename as ``, {filename}'' */ |
887 |
length += 2 + filename_length; |
length += 2 + filename_length; |
899 |
} |
} |
900 |
} |
} |
901 |
|
|
902 |
cp = chars = (jschar *) JS_malloc(cx, (length + 1) * sizeof(jschar)); |
cp = chars = (jschar *) cx->malloc((length + 1) * sizeof(jschar)); |
903 |
if (!chars) { |
if (!chars) { |
904 |
ok = JS_FALSE; |
ok = JS_FALSE; |
905 |
goto out; |
goto out; |
906 |
} |
} |
907 |
|
|
908 |
*cp++ = '('; *cp++ = 'n'; *cp++ = 'e'; *cp++ = 'w'; *cp++ = ' '; |
*cp++ = '('; *cp++ = 'n'; *cp++ = 'e'; *cp++ = 'w'; *cp++ = ' '; |
909 |
js_strncpy(cp, JSSTRING_CHARS(name), name_length); |
js_strncpy(cp, name->chars(), name_length); |
910 |
cp += name_length; |
cp += name_length; |
911 |
*cp++ = '('; |
*cp++ = '('; |
912 |
if (message_length != 0) { |
if (message_length != 0) { |
913 |
js_strncpy(cp, JSSTRING_CHARS(message), message_length); |
js_strncpy(cp, message->chars(), message_length); |
914 |
cp += message_length; |
cp += message_length; |
915 |
} |
} |
916 |
|
|
917 |
if (filename_length != 0) { |
if (filename_length != 0) { |
918 |
/* append filename as ``, {filename}'' */ |
/* append filename as ``, {filename}'' */ |
919 |
*cp++ = ','; *cp++ = ' '; |
*cp++ = ','; *cp++ = ' '; |
920 |
js_strncpy(cp, JSSTRING_CHARS(filename), filename_length); |
js_strncpy(cp, filename->chars(), filename_length); |
921 |
cp += filename_length; |
cp += filename_length; |
922 |
} else { |
} else { |
923 |
if (lineno_as_str) { |
if (lineno_as_str) { |
931 |
if (lineno_as_str) { |
if (lineno_as_str) { |
932 |
/* append lineno as ``, {lineno_as_str}'' */ |
/* append lineno as ``, {lineno_as_str}'' */ |
933 |
*cp++ = ','; *cp++ = ' '; |
*cp++ = ','; *cp++ = ' '; |
934 |
js_strncpy(cp, JSSTRING_CHARS(lineno_as_str), lineno_length); |
js_strncpy(cp, lineno_as_str->chars(), lineno_length); |
935 |
cp += lineno_length; |
cp += lineno_length; |
936 |
} |
} |
937 |
|
|
939 |
|
|
940 |
result = js_NewString(cx, chars, length); |
result = js_NewString(cx, chars, length); |
941 |
if (!result) { |
if (!result) { |
942 |
JS_free(cx, chars); |
cx->free(chars); |
943 |
ok = JS_FALSE; |
ok = JS_FALSE; |
944 |
goto out; |
goto out; |
945 |
} |
} |
1017 |
/* Make the prototype for the current constructor name. */ |
/* Make the prototype for the current constructor name. */ |
1018 |
proto = js_NewObject(cx, &js_ErrorClass, |
proto = js_NewObject(cx, &js_ErrorClass, |
1019 |
(i != JSEXN_ERR) ? error_proto : obj_proto, |
(i != JSEXN_ERR) ? error_proto : obj_proto, |
1020 |
obj, 0); |
obj); |
1021 |
if (!proto) |
if (!proto) |
1022 |
return NULL; |
return NULL; |
1023 |
if (i == JSEXN_ERR) { |
if (i == JSEXN_ERR) { |
1030 |
} |
} |
1031 |
|
|
1032 |
/* So exn_finalize knows whether to destroy private data. */ |
/* So exn_finalize knows whether to destroy private data. */ |
1033 |
STOBJ_SET_SLOT(proto, JSSLOT_PRIVATE, JSVAL_VOID); |
proto->setPrivate(NULL); |
1034 |
|
|
1035 |
/* Make a constructor function for the current name. */ |
/* Make a constructor function for the current name. */ |
1036 |
protoKey = GetExceptionProtoKey(i); |
protoKey = GetExceptionProtoKey(i); |
1169 |
goto out; |
goto out; |
1170 |
tv[0] = OBJECT_TO_JSVAL(errProto); |
tv[0] = OBJECT_TO_JSVAL(errProto); |
1171 |
|
|
1172 |
errObject = js_NewObject(cx, &js_ErrorClass, errProto, NULL, 0); |
errObject = js_NewObject(cx, &js_ErrorClass, errProto, NULL); |
1173 |
if (!errObject) { |
if (!errObject) { |
1174 |
ok = JS_FALSE; |
ok = JS_FALSE; |
1175 |
goto out; |
goto out; |