| 1 | /* -*- Mode: C; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*- | |
| 2 | * | |
| 3 | * ***** BEGIN LICENSE BLOCK ***** | |
| 4 | * Version: MPL 1.1/GPL 2.0/LGPL 2.1 | |
| 5 | * | |
| 6 | * The contents of this file are subject to the Mozilla Public License Version | |
| 7 | * 1.1 (the "License"); you may not use this file except in compliance with | |
| 8 | * the License. You may obtain a copy of the License at | |
| 9 | * http://www.mozilla.org/MPL/ | |
| 10 | * | |
| 11 | * Software distributed under the License is distributed on an "AS IS" basis, | |
| 12 | * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License | |
| 13 | * for the specific language governing rights and limitations under the | |
| 14 | * License. | |
| 15 | * | |
| 16 | * The Original Code is Mozilla Communicator client code, released | |
| 17 | * March 31, 1998. | |
| 18 | * | |
| 19 | * The Initial Developer of the Original Code is | |
| 20 | * Netscape Communications Corporation. | |
| 21 | * Portions created by the Initial Developer are Copyright (C) 1998 | |
| 22 | * the Initial Developer. All Rights Reserved. | |
| 23 | * | |
| 24 | * Contributor(s): | |
| 25 | * IBM Corp. | |
| 26 | * | |
| 27 | * Alternatively, the contents of this file may be used under the terms of | |
| 28 | * either of the GNU General Public License Version 2 or later (the "GPL"), | |
| 29 | * or the GNU Lesser General Public License Version 2.1 or later (the "LGPL"), | |
| 30 | * in which case the provisions of the GPL or the LGPL are applicable instead | |
| 31 | * of those above. If you wish to allow use of your version of this file only | |
| 32 | * under the terms of either the GPL or the LGPL, and not to allow others to | |
| 33 | * use your version of this file under the terms of the MPL, indicate your | |
| 34 | * decision by deleting the provisions above and replace them with the notice | |
| 35 | * and other provisions required by the GPL or the LGPL. If you do not delete | |
| 36 | * the provisions above, a recipient may use your version of this file under | |
| 37 | * the terms of any one of the MPL, the GPL or the LGPL. | |
| 38 | * | |
| 39 | * ***** END LICENSE BLOCK ***** */ | |
| 40 | ||
| 41 | /* | |
| 42 | * PR assertion checker. | |
| 43 | */ | |
| 44 | #include "jsstddef.h" | |
| 45 | #include <stdio.h> | |
| 46 | #include <stdlib.h> | |
| 47 | #include "jstypes.h" | |
| 48 | #include "jsutil.h" | |
| 49 | ||
| 50 | #ifdef WIN32 | |
| 51 | # include <windows.h> | |
| 52 | #endif | |
| 53 | ||
| 54 | #ifdef XP_MAC | |
| 55 | # include <Types.h> | |
| 56 | # include <stdarg.h> | |
| 57 | # include "jsprf.h" | |
| 58 | #endif | |
| 59 | ||
| 60 | #ifdef XP_MAC | |
| 61 | /* | |
| 62 | * PStrFromCStr converts the source C string to a destination | |
| 63 | * pascal string as it copies. The dest string will | |
| 64 | * be truncated to fit into an Str255 if necessary. | |
| 65 | * If the C String pointer is NULL, the pascal string's length is | |
| 66 | * set to zero. | |
| 67 | */ | |
| 68 | static void PStrFromCStr(const char *src, Str255 dst) | |
| 69 | { | |
| 70 | short length = 0; | |
| 71 | ||
| 72 | /* handle case of overlapping strings */ | |
| 73 | if ( (void*)src == (void*)dst ) | |
| 74 | { | |
| 75 | unsigned char *curdst = &dst[1]; | |
| 76 | unsigned char thisChar; | |
| 77 | ||
| 78 | thisChar = *(const unsigned char*)src++; | |
| 79 | while ( thisChar != '\0' ) | |
| 80 | { | |
| 81 | unsigned char nextChar; | |
| 82 | ||
| 83 | /* | |
| 84 | * Use nextChar so we don't overwrite what we | |
| 85 | * are about to read | |
| 86 | */ | |
| 87 | nextChar = *(const unsigned char*)src++; | |
| 88 | *curdst++ = thisChar; | |
| 89 | thisChar = nextChar; | |
| 90 | ||
| 91 | if ( ++length >= 255 ) | |
| 92 | break; | |
| 93 | } | |
| 94 | } | |
| 95 | else if ( src != NULL ) | |
| 96 | { | |
| 97 | unsigned char *curdst = &dst[1]; | |
| 98 | /* count down so test it loop is faster */ | |
| 99 | short overflow = 255; | |
| 100 | register char temp; | |
| 101 | ||
| 102 | /* | |
| 103 | * Can't do the K&R C thing of while (*s++ = *t++) | |
| 104 | * because it will copy trailing zero which might | |
| 105 | * overrun pascal buffer. Instead we use a temp variable. | |
| 106 | */ | |
| 107 | while ( (temp = *src++) != 0 ) | |
| 108 | { | |
| 109 | *(char*)curdst++ = temp; | |
| 110 | ||
| 111 | if ( --overflow <= 0 ) | |
| 112 | break; | |
| 113 | } | |
| 114 | length = 255 - overflow; | |
| 115 | } | |
| 116 | dst[0] = length; | |
| 117 | } | |
| 118 | ||
| 119 | static void jsdebugstr(const char *debuggerMsg) | |
| 120 | { | |
| 121 | Str255 pStr; | |
| 122 | ||
| 123 | PStrFromCStr(debuggerMsg, pStr); | |
| 124 | DebugStr(pStr); | |
| 125 | } | |
| 126 | ||
| 127 | static void dprintf(const char *format, ...) | |
| 128 | { | |
| 129 | va_list ap; | |
| 130 | char *buffer; | |
| 131 | ||
| 132 | va_start(ap, format); | |
| 133 | buffer = (char *)JS_vsmprintf(format, ap); | |
| 134 | va_end(ap); | |
| 135 | ||
| 136 | jsdebugstr(buffer); | |
| 137 | JS_smprintf_free(buffer); | |
| 138 | } | |
| 139 | #endif /* XP_MAC */ | |
| 140 | ||
| 141 | JS_PUBLIC_API(void) JS_Assert(const char *s, const char *file, JSIntn ln) | |
| 142 | 0 | { |
| 143 | #ifdef XP_MAC | |
| 144 | dprintf("Assertion failure: %s, at %s:%d\n", s, file, ln); | |
| 145 | #else | |
| 146 | 0 | fprintf(stderr, "Assertion failure: %s, at %s:%d\n", s, file, ln); |
| 147 | #endif | |
| 148 | #if defined(WIN32) | |
| 149 | DebugBreak(); | |
| 150 | #endif | |
| 151 | #if defined(XP_OS2) | |
| 152 | asm("int $3"); | |
| 153 | #endif | |
| 154 | #ifndef XP_MAC | |
| 155 | 0 | abort(); |
| 156 | #endif |