Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 /* | 1 /* |
| 2 * Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file | 2 * Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file |
| 3 * for details. All rights reserved. Use of this source code is governed by a | 3 * for details. All rights reserved. Use of this source code is governed by a |
| 4 * BSD-style license that can be found in the LICENSE file. | 4 * BSD-style license that can be found in the LICENSE file. |
| 5 */ | 5 */ |
| 6 | 6 |
| 7 #ifndef INCLUDE_DART_API_H_ | 7 #ifndef INCLUDE_DART_API_H_ |
| 8 #define INCLUDE_DART_API_H_ | 8 #define INCLUDE_DART_API_H_ |
| 9 | 9 |
| 10 /** \mainpage Dart Embedding API Reference | 10 /** \mainpage Dart Embedding API Reference |
| (...skipping 126 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 137 * normal fatal error. | 137 * normal fatal error. |
| 138 * | 138 * |
| 139 * --- Propagating errors --- | 139 * --- Propagating errors --- |
| 140 * | 140 * |
| 141 * When an error handle is returned from the top level invocation of | 141 * When an error handle is returned from the top level invocation of |
| 142 * Dart code in a program, the embedder must handle the error as they | 142 * Dart code in a program, the embedder must handle the error as they |
| 143 * see fit. Often, the embedder will print the error message produced | 143 * see fit. Often, the embedder will print the error message produced |
| 144 * by Dart_Error and exit the program. | 144 * by Dart_Error and exit the program. |
| 145 * | 145 * |
| 146 * When an error is returned while in the body of a native function, | 146 * When an error is returned while in the body of a native function, |
| 147 * it can be propagated by calling Dart_PropagateError. Errors should | 147 * it can be propagated up the call stack by calling |
| 148 * be propagated unless there is a specific reason not to. If an | 148 * Dart_PropagateError, Dart_SetReturnValue, or Dart_ThrowException. |
| 149 * error is not propagated then it is ignored. For example, if an | 149 * Errors should be propagated unless there is a specific reason not |
| 150 * unhandled exception error is ignored, that effectively "catches" | 150 * to. If an error is not propagated then it is ignored. For |
| 151 * the unhandled exception. Fatal errors must always be propagated. | 151 * example, if an unhandled exception error is ignored, that |
| 152 * effectively "catches" the unhandled exception. Fatal errors must | |
| 153 * always be propagated. | |
| 152 * | 154 * |
| 153 * Note that a call to Dart_PropagateError never returns. Instead it | 155 * When an error is propagated, any current scopes created by |
| 154 * transfers control non-locally using a setjmp-like mechanism. This | 156 * Dart_EnterScope will be exited. |
| 155 * can be inconvenient if you have resources that you need to clean up | |
| 156 * before propagating the error. When an error is propagated, any | |
| 157 * current scopes created by Dart_EnterScope will be exited. | |
| 158 * | 157 * |
| 159 * To deal with this inconvenience, we often return error handles | 158 * Dart_PropagateError and Dart_ThrowException do not return. Instead |
| 159 * they transfer control non-locally using a setjmp-like mechanism. | |
| 160 * This can be inconvenient if you have resources that you need to | |
| 161 * clean up before propagating the error. | |
| 162 * | |
| 163 * When relying on Dart_PropagateError, we often return error handles | |
| 160 * rather than propagating them from helper functions. Consider the | 164 * rather than propagating them from helper functions. Consider the |
| 161 * following contrived example: | 165 * following contrived example: |
| 162 * | 166 * |
| 163 * 1 Dart_Handle isLongStringHelper(Dart_Handle arg) { | 167 * 1 Dart_Handle isLongStringHelper(Dart_Handle arg) { |
| 164 * 2 intptr_t* length = 0; | 168 * 2 intptr_t* length = 0; |
| 165 * 3 result = Dart_StringLength(arg, &length); | 169 * 3 result = Dart_StringLength(arg, &length); |
| 166 * 4 if (Dart_IsError(result)) { | 170 * 4 if (Dart_IsError(result)) { |
| 167 * 5 return result | 171 * 5 return result |
| 168 * 6 } | 172 * 6 } |
| 169 * 7 return Dart_NewBoolean(length > 100); | 173 * 7 return Dart_NewBoolean(length > 100); |
| (...skipping 14 matching lines...) Expand all Loading... | |
| 184 * 22 Dart_ExitScope(); | 188 * 22 Dart_ExitScope(); |
| 185 * 23 } | 189 * 23 } |
| 186 * | 190 * |
| 187 * In this example, we have a native function which calls a helper | 191 * In this example, we have a native function which calls a helper |
| 188 * function to do its work. On line 5, the helper function could call | 192 * function to do its work. On line 5, the helper function could call |
| 189 * Dart_PropagateError, but that would not give the native function a | 193 * Dart_PropagateError, but that would not give the native function a |
| 190 * chance to call FreeMyResource(), causing a leak. Instead, the | 194 * chance to call FreeMyResource(), causing a leak. Instead, the |
| 191 * helper function returns the error handle to the caller, giving the | 195 * helper function returns the error handle to the caller, giving the |
| 192 * caller a chance to clean up before propagating the error handle. | 196 * caller a chance to clean up before propagating the error handle. |
| 193 * | 197 * |
| 198 * When an error is propagated by calling Dart_SetReturnValue, the | |
| 199 * native function will be allowed to complete normally and then the | |
| 200 * exception will be propagated only once the native call | |
| 201 * returns. This can be convenient, as it allows the C code to clean | |
| 202 * up normally. | |
| 203 * | |
| 204 * The example can be written more simply using Dart_SetReturnValue to | |
| 205 * propagate the error. | |
| 206 * | |
| 207 * 1 Dart_Handle isLongStringHelper(Dart_Handle arg) { | |
| 208 * 2 intptr_t* length = 0; | |
| 209 * 3 result = Dart_StringLength(arg, &length); | |
| 210 * 4 if (Dart_IsError(result)) { | |
| 211 * 5 return result | |
| 212 * 6 } | |
| 213 * 7 return Dart_NewBoolean(length > 100); | |
| 214 * 8 } | |
| 215 * 9 | |
| 216 * 10 void NativeFunction_isLongString(Dart_NativeArguments args) { | |
| 217 * 11 Dart_EnterScope(); | |
| 218 * 12 AllocateMyResource(); | |
| 219 * 13 Dart_Handle arg = Dart_GetNativeArgument(args, 0); | |
| 220 * 14 Dart_SetReturnValue(isLongStringHelper(arg)); | |
| 221 * 15 FreeMyResource(); | |
| 222 * 16 Dart_ExitScope(); | |
| 223 * 17 } | |
| 224 * | |
| 225 * In this example, the call to Dart_SetReturnValue on line 14 will | |
| 226 * either return the normal return value or propagate the error result | |
|
siva
2016/02/03 16:50:00
The wording 'propagate the error' seems to imply t
turnidge
2016/02/03 19:13:46
Done.
| |
| 227 * (potentially generated on line 3). The call to FreeMyResource on | |
| 228 * line 15 will execute in either case. | |
| 229 * | |
| 194 * --- Local and persistent handles --- | 230 * --- Local and persistent handles --- |
| 195 * | 231 * |
| 196 * Local handles are allocated within the current scope (see | 232 * Local handles are allocated within the current scope (see |
| 197 * Dart_EnterScope) and go away when the current scope exits. Unless | 233 * Dart_EnterScope) and go away when the current scope exits. Unless |
| 198 * otherwise indicated, callers should assume that all functions in | 234 * otherwise indicated, callers should assume that all functions in |
| 199 * the Dart embedding api return local handles. | 235 * the Dart embedding api return local handles. |
| 200 * | 236 * |
| 201 * Persistent handles are allocated within the current isolate. They | 237 * Persistent handles are allocated within the current isolate. They |
| 202 * can be used to store objects across scopes. Persistent handles have | 238 * can be used to store objects across scopes. Persistent handles have |
| 203 * the lifetime of the current isolate unless they are explicitly | 239 * the lifetime of the current isolate unless they are explicitly |
| (...skipping 1932 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 2136 * uses with Dart_NewUnhandledExceptionError. */ | 2172 * uses with Dart_NewUnhandledExceptionError. */ |
| 2137 | 2173 |
| 2138 /** | 2174 /** |
| 2139 * Throws an exception. | 2175 * Throws an exception. |
| 2140 * | 2176 * |
| 2141 * This function causes a Dart language exception to be thrown. This | 2177 * This function causes a Dart language exception to be thrown. This |
| 2142 * will proceed in the standard way, walking up Dart frames until an | 2178 * will proceed in the standard way, walking up Dart frames until an |
| 2143 * appropriate 'catch' block is found, executing 'finally' blocks, | 2179 * appropriate 'catch' block is found, executing 'finally' blocks, |
| 2144 * etc. | 2180 * etc. |
| 2145 * | 2181 * |
| 2182 * If an error handle is passed into this function, the error is | |
| 2183 * propagated immediately. See Dart_PropagateError for a discussion | |
| 2184 * of error propagation. | |
| 2185 * | |
| 2146 * If successful, this function does not return. Note that this means | 2186 * If successful, this function does not return. Note that this means |
| 2147 * that the destructors of any stack-allocated C++ objects will not be | 2187 * that the destructors of any stack-allocated C++ objects will not be |
| 2148 * called. If there are no Dart frames on the stack, an error occurs. | 2188 * called. If there are no Dart frames on the stack, an error occurs. |
| 2149 * | 2189 * |
| 2150 * \return An error handle if the exception was not thrown. | 2190 * \return An error handle if the exception was not thrown. |
| 2151 * Otherwise the function does not return. | 2191 * Otherwise the function does not return. |
| 2152 */ | 2192 */ |
| 2153 DART_EXPORT Dart_Handle Dart_ThrowException(Dart_Handle exception); | 2193 DART_EXPORT Dart_Handle Dart_ThrowException(Dart_Handle exception); |
| 2154 | 2194 |
| 2155 /** | 2195 /** |
| (...skipping 217 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 2373 * \param arg_index Index of the desired argument in the structure above. | 2413 * \param arg_index Index of the desired argument in the structure above. |
| 2374 * \param value Returns the double value if the argument is a double. | 2414 * \param value Returns the double value if the argument is a double. |
| 2375 * \return Success if no error occurs. Otherwise returns an error handle. | 2415 * \return Success if no error occurs. Otherwise returns an error handle. |
| 2376 */ | 2416 */ |
| 2377 DART_EXPORT Dart_Handle Dart_GetNativeDoubleArgument(Dart_NativeArguments args, | 2417 DART_EXPORT Dart_Handle Dart_GetNativeDoubleArgument(Dart_NativeArguments args, |
| 2378 int index, | 2418 int index, |
| 2379 double* value); | 2419 double* value); |
| 2380 | 2420 |
| 2381 /** | 2421 /** |
| 2382 * Sets the return value for a native function. | 2422 * Sets the return value for a native function. |
| 2423 * | |
| 2424 * If retval is an Error handle, then error will be propagated once | |
| 2425 * the native functions exits. See Dart_PropagateError for a | |
| 2426 * discussion of how different types of errors are propagated. | |
|
siva
2016/02/03 16:50:00
I think the obvious question that comes up is when
turnidge
2016/02/03 19:13:46
Yes. I've added this paragraph to the discussion
| |
| 2383 */ | 2427 */ |
| 2384 DART_EXPORT void Dart_SetReturnValue(Dart_NativeArguments args, | 2428 DART_EXPORT void Dart_SetReturnValue(Dart_NativeArguments args, |
| 2385 Dart_Handle retval); | 2429 Dart_Handle retval); |
| 2386 | 2430 |
| 2387 DART_EXPORT void Dart_SetWeakHandleReturnValue(Dart_NativeArguments args, | 2431 DART_EXPORT void Dart_SetWeakHandleReturnValue(Dart_NativeArguments args, |
| 2388 Dart_WeakPersistentHandle rval); | 2432 Dart_WeakPersistentHandle rval); |
| 2389 | 2433 |
| 2390 DART_EXPORT void Dart_SetBooleanReturnValue(Dart_NativeArguments args, | 2434 DART_EXPORT void Dart_SetBooleanReturnValue(Dart_NativeArguments args, |
| 2391 bool retval); | 2435 bool retval); |
| 2392 | 2436 |
| (...skipping 412 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 2805 intptr_t* vm_isolate_snapshot_size, | 2849 intptr_t* vm_isolate_snapshot_size, |
| 2806 uint8_t** isolate_snapshot_buffer, | 2850 uint8_t** isolate_snapshot_buffer, |
| 2807 intptr_t* isolate_snapshot_size, | 2851 intptr_t* isolate_snapshot_size, |
| 2808 uint8_t** instructions_snapshot_buffer, | 2852 uint8_t** instructions_snapshot_buffer, |
| 2809 intptr_t* instructions_snapshot_size); | 2853 intptr_t* instructions_snapshot_size); |
| 2810 | 2854 |
| 2811 | 2855 |
| 2812 DART_EXPORT bool Dart_IsRunningPrecompiledCode(); | 2856 DART_EXPORT bool Dart_IsRunningPrecompiledCode(); |
| 2813 | 2857 |
| 2814 #endif /* INCLUDE_DART_API_H_ */ /* NOLINT */ | 2858 #endif /* INCLUDE_DART_API_H_ */ /* NOLINT */ |
| OLD | NEW |