source: webkit/trunk/JavaScriptCore/API/JSValueRef.h@ 15149

Last change on this file since 15149 was 15149, checked in by ggaren, 19 years ago

Reviewed by Darin.


  • Refined value conversions in the API:
    • failed toNumber returns NaN
    • failed toObject returns NULL
    • failed toString returns empty string


  • Refined excpetion handling in the API:
    • failed value conversions do not throw exceptions
    • uncaught exceptions in JSEvaluate, JSObjectCallAsFunction, and JSObjectCallAsConstructor are returned through a JSValueRef* exception argument
    • removed JSContextHasException, because JSContextGetException does the same job


  • API/JSBase.h:
  • API/JSCharBufferRef.cpp: (JSValueCopyStringValue):
  • API/JSContextRef.cpp: (JSEvaluate):
  • API/JSContextRef.h:
  • API/JSNodeList.c: Added test code demonstrating how you would use toNumber, and why you probably don't need toUInt32, etc. (JSNodeListPrototype_item): (JSNodeList_getProperty):
  • API/JSObjectRef.cpp: (JSValueToObject): (JSObjectCallAsFunction): (JSObjectCallAsConstructor):
  • API/JSObjectRef.h:
  • API/JSValueRef.cpp: (JSValueToNumber):
  • API/JSValueRef.h:
  • API/minidom.c: (main):
  • API/testapi.c: (main): Added tests for new rules, and call to JSGCProtect to fix Intel crash
  • JavaScriptCore.exp:
File size: 7.4 KB
Line 
1// -*- mode: c++; c-basic-offset: 4 -*-
2/*
3 * Copyright (C) 2006 Apple Computer, Inc. All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 *
14 * THIS SOFTWARE IS PROVIDED BY APPLE COMPUTER, INC. ``AS IS'' AND ANY
15 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
17 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE COMPUTER, INC. OR
18 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
19 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
20 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
21 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
22 * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
24 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25 */
26
27#ifndef JSValueRef_h
28#define JSValueRef_h
29
30#include "JSBase.h"
31
32/*!
33 @enum JSTypeCode
34 A constant identifying the type of a particular JSValueRef.
35 @constant kJSTypeUndefined the unique undefined value
36 @constant kJSTypeNull the unique null value
37 @constant kJSBoolean a primitive boolean value, one of true or false
38 @constant kJSTypeNumber a primitive number value
39 @constant kJSTypeString a primitive string value
40 @constant kJSTypeObject an object (meaning this JSValueRef is a JSObjectRef
41*/
42typedef enum {
43 kJSTypeUndefined,
44 kJSTypeNull,
45 kJSTypeBoolean,
46 kJSTypeNumber,
47 kJSTypeString,
48 kJSTypeObject
49} JSTypeCode;
50
51#ifdef __cplusplus
52extern "C" {
53#endif
54
55/*!
56 @function JSValueGetType
57 Get the type code for a particular JavaScript value
58 @param value the JS value for which the type should be determined
59 @result a type code identifying the type
60*/
61JSTypeCode JSValueGetType(JSValueRef value);
62
63/*!
64 @function JSValueIsUndefined
65 Determine if value is of type undefined
66 @param value the JS value to check for undefined type
67 @result true if the value is undefined, false otherwise
68*/
69bool JSValueIsUndefined(JSValueRef value);
70
71/*!
72 @function JSValueIsNull
73 Determine if value is of type null
74 @param value the JS value to check for null type
75 @result true if the value is null, false otherwise
76*/
77bool JSValueIsNull(JSValueRef value);
78
79/*!
80 @function JSValueIsBoolean
81 Determine if value is of type boolean
82 @param value the JS value to check for boolean type
83 @result true if the value is a boolean, false otherwise
84*/
85bool JSValueIsBoolean(JSValueRef value);
86
87/*!
88 @function JSValueIsNumber
89 Determine if value is of type number
90 @param value the JS value to check for number type
91 @result true if the value is a number, false otherwise
92*/
93bool JSValueIsNumber(JSValueRef value);
94
95/*!
96 @function JSValueIsString
97 Determine if value is of type string
98 @param value the JS value to check for string type
99 @result true if the value is a string, false otherwise
100*/
101bool JSValueIsString(JSValueRef value);
102
103/*!
104 @function JSValueIsObject
105 Determine if value is of type object
106 @param value the JS value to check for object type
107 @result true if the value is an object, false otherwise
108*/
109bool JSValueIsObject(JSValueRef value);
110bool JSValueIsObjectOfClass(JSValueRef value, JSClassRef jsClass);
111
112// Comparing values
113
114/*!
115 @function JSValueIsEqual
116 Check if two values are equal by JavaScript rules, as if compared by the JS == operator
117 @param context the execution context to use
118 @param a the first value to compare
119 @param b the second value to compare
120 @result true if the two values are equal, false otherwise
121*/
122bool JSValueIsEqual(JSContextRef context, JSValueRef a, JSValueRef b);
123
124/*!
125 @function JSValueIsStrictEqual
126 Check if two values are strict equal by JavaScript rules, as if compared by the JS === operator
127 @param context the execution context to use
128 @param a the first value to compare
129 @param b the second value to compare
130 @result true if the two values are strict equal, false otherwise
131*/
132bool JSValueIsStrictEqual(JSContextRef context, JSValueRef a, JSValueRef b);
133
134/*!
135 @function JSValueIsInstanceOf
136 Check if a value is an instance of a particular object; generally this means the object
137 was used as the constructor for that instance
138 @param context the execution context to use
139 @param value the possible instance
140 @param object the possible constructor
141 @result true if value is an instance of object
142*/
143bool JSValueIsInstanceOf(JSContextRef context, JSValueRef value, JSObjectRef object);
144
145// Creating values
146
147/*!
148 @function JSUndefinedMake
149 Make a value of the undefined type.
150 @result The unique undefined value.
151*/
152JSValueRef JSUndefinedMake(void);
153
154/*!
155 @function JSNullMake
156 Make a value of the null type.
157 @result the unique null value
158*/
159JSValueRef JSNullMake(void);
160
161/*!
162 @function JSBooleanMake
163 Make a value of the boolean type.
164 @param value whether the returned value should be true or false
165 @result a JS true or false boolean value, as appropriate
166*/
167
168JSValueRef JSBooleanMake(bool value);
169
170/*!
171 @function JSNumberMake
172 Make a value of the number type.
173 @param value the numberic value of the number to make
174 @result a JS number corresponding to value
175*/
176JSValueRef JSNumberMake(double value);
177
178/*!
179 @function JSStringMake
180 Make a value of the string type.
181 @param buffer the internal string contents for the string value
182 @result a JS string value that has the value of the buffer
183*/
184JSValueRef JSStringMake(JSCharBufferRef buffer);
185
186// Converting to primitive values
187
188/*!
189 @function JSValueToBoolean
190 Convert a JavaScript value to boolean and return the resulting boolean
191 @param context the execution context to use
192 @param value the value to convert
193 @result the boolean result of conversion
194*/
195bool JSValueToBoolean(JSContextRef context, JSValueRef value);
196
197/*!
198 @function JSValueToNumber
199 Convert a JavaScript value to number and return the resulting number
200 @param context the execution context to use
201 @param value the value to convert
202 @result the numeric result of conversion, or NaN if conversion fails
203*/
204double JSValueToNumber(JSContextRef context, JSValueRef value);
205
206/*!
207 @function JSValueCopyStringValue
208 Convert a JavaScript value to string and copy the resulting string into a newly allocated character buffer
209 @param context the execution context to use
210 @param value the value to convert
211 @result a character buffer containing the result of conversion, or an empty character buffer if conversion fails
212*/
213JSCharBufferRef JSValueCopyStringValue(JSContextRef context, JSValueRef value);
214
215/*!
216 @function JSValueToObject
217 Convert a JavaScript value to object and return the resulting object
218 @param context the execution context to use
219 @param value the value to convert
220 @result the object result of conversion, or NULL if conversion fails
221*/
222JSObjectRef JSValueToObject(JSContextRef context, JSValueRef value);
223
224#ifdef __cplusplus
225}
226#endif
227
228#endif // JSValueRef_h
Note: See TracBrowser for help on using the repository browser.