My Project
jsni.h
Go to the documentation of this file.
1 // JavaScript Native Interface Release License.
2 //
3 // Copyright (c) 2015-2017 Alibaba Group. 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 are met:
7 // * Redistributions of source code must retain the above copyright
8 // notice, this list of conditions and the following disclaimer.
9 // * Redistributions in binary form must reproduce the above copyright
10 // notice, this list of conditions and the following disclaimer in the
11 // documentation and/or other materials provided with the distribution.
12 // * Neither the name of the Alibaba Group nor the
13 // names of its contributors may be used to endorse or promote products
14 // derived from this software without specific prior written permission.
15 //
16 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
17 // ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
18 // WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
19 // DISCLAIMED. IN NO EVENT SHALL <COPYRIGHT HOLDER> BE LIABLE FOR ANY
20 // DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
21 // (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
22 // LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
23 // ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
25 // SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26 
27 
35 #ifndef INCLUDE_JSNI_H_
36 #define INCLUDE_JSNI_H_
37 
38 // For size_t.
39 #include <stddef.h>
40 // For bool.
41 #include <stdbool.h>
42 // For uint16_t.
43 #include <stdint.h>
44 
48 typedef struct _JSValueRef* JSValueRef;
49 
53 typedef struct _JSNIEnv JSNIEnv;
54 
58 typedef void (*JSNIGCCallback)(JSNIEnv*, void*);
59 
63 typedef struct _JSNICallbackInfo* JSNICallbackInfo;
64 
68 typedef void (*JSNICallback)(JSNIEnv*, const JSNICallbackInfo);
69 
73 typedef struct _JSGlobalValueRef* JSGlobalValueRef;
74 
78 typedef enum {
102 
104 typedef enum {
106  JSNINone = 0,
108  JSNIReadOnly = 1 << 0,
110  JSNIDontEnum = 1 << 1,
112  JSNIDontDelete = 1 << 2
114 
116 typedef struct {
122 
124 typedef struct {
132  void* data;
134 
136 typedef struct {
142 
144 typedef enum {
149 } JSNIErrorCode;
150 
152 typedef struct {
154  const char* msg;
157 } JSNIErrorInfo;
158 
159 
160 #if defined(__cplusplus)
161 extern "C" {
162 #endif
163 
173 int JSNIGetVersion(JSNIEnv* env);
174 
183 bool JSNIRegisterMethod(JSNIEnv* env, const JSValueRef recv, const char* name, JSNICallback callback);
184 
192 
201 
210 
218 
227 
234 bool JSNIIsUndefined(JSNIEnv* env, JSValueRef val);
235 
242 
249 bool JSNIIsNull(JSNIEnv* env, JSValueRef val);
250 
257 
264 bool JSNIIsBoolean(JSNIEnv* env, JSValueRef val);
265 
272 bool JSNIToCBool(JSNIEnv* env, JSValueRef val);
273 
280 JSValueRef JSNINewBoolean(JSNIEnv* env, bool val);
281 
288 bool JSNIIsNumber(JSNIEnv* env, JSValueRef val);
289 
296 JSValueRef JSNINewNumber(JSNIEnv* env, double val);
297 
304 double JSNIToCDouble(JSNIEnv* env, JSValueRef val);
305 
313 int32_t JSNIToInt32(JSNIEnv* env, JSValueRef val);
314 
322 uint32_t JSNIToUint32(JSNIEnv* env, JSValueRef val);
323 
331 int64_t JSNIToInt64(JSNIEnv* env, JSValueRef val);
332 
339 bool JSNIIsSymbol(JSNIEnv* env, JSValueRef val);
340 
348 
355 bool JSNIIsString(JSNIEnv* env, JSValueRef val);
356 
366 JSValueRef JSNINewStringFromUtf8(JSNIEnv* env, const char* src, size_t length);
367 
374 size_t JSNIGetStringUtf8Length(JSNIEnv* env, JSValueRef string);
375 
386 size_t JSNIGetStringUtf8Chars(JSNIEnv* env, JSValueRef string, char* copy, size_t length);
387 
397 JSValueRef JSNINewString(JSNIEnv* env, const uint16_t* src, size_t length);
398 
406 size_t JSNIGetStringLength(JSNIEnv* env, JSValueRef string);
407 
418 size_t JSNIGetString(JSNIEnv* env, JSValueRef string, uint16_t* copy, size_t length);
419 
426 bool JSNIIsObject(JSNIEnv* env, JSValueRef val);
427 
434 bool JSNIIsEmpty(JSNIEnv* env, JSValueRef val);
435 
442 
450 bool JSNIHasProperty(JSNIEnv* env, JSValueRef object, const char* name);
451 
459 JSValueRef JSNIGetProperty(JSNIEnv* env, JSValueRef object, const char* name);
460 
469 bool JSNISetProperty(JSNIEnv* env, JSValueRef object, const char* name, JSValueRef property);
470 
480 bool JSNIDefineProperty(JSNIEnv* env, JSValueRef object, const char* name, const JSNIPropertyDescriptor descriptor);
481 
489 bool JSNIDeleteProperty(JSNIEnv* env, JSValueRef object, const char* name);
490 
498 
508 
515 int JSNIInternalFieldCount(JSNIEnv* env, JSValueRef object);
516 
525 void JSNISetInternalField(JSNIEnv* env, JSValueRef object, int index, void* field);
526 
534 void* JSNIGetInternalField(JSNIEnv* env, JSValueRef object, int index);
535 
542 bool JSNIIsFunction(JSNIEnv* env, JSValueRef val);
543 
551 
561 JSValueRef JSNICallFunction(JSNIEnv* env, JSValueRef func, JSValueRef recv, int argc, JSValueRef* argv);
562 
569 bool JSNIIsArray(JSNIEnv* env, JSValueRef val);
570 
577 size_t JSNIGetArrayLength(JSNIEnv* env, JSValueRef array);
578 
585 JSValueRef JSNINewArray(JSNIEnv* env, size_t initial_length);
586 
594 JSValueRef JSNIGetArrayElement(JSNIEnv* env, JSValueRef array, size_t index);
595 
604 void JSNISetArrayElement(JSNIEnv* env, JSValueRef array, size_t index, JSValueRef value);
605 
612 bool JSNIIsTypedArray(JSNIEnv* env, JSValueRef val);
613 
622 JSValueRef JSNINewTypedArray(JSNIEnv* env, JsTypedArrayType type, void* data, size_t length);
623 
631 
638 void* JSNIGetTypedArrayData(JSNIEnv* env, JSValueRef typed_array);
639 
646 size_t JSNIGetTypedArrayLength(JSNIEnv* env, JSValueRef typed_array);
647 
656 void JSNIPushLocalScope(JSNIEnv* env);
657 
664 void JSNIPopLocalScope(JSNIEnv* env);
665 
675 
685 
698 
706 
717 
728 
736 
747 void JSNISetGCCallback(JSNIEnv* env, JSGlobalValueRef val, void* args, JSNIGCCallback callback);
748 
756 void JSNIThrowErrorException(JSNIEnv* env, const char* errmsg);
757 
765 void JSNIThrowTypeErrorException(JSNIEnv* env, const char* errmsg);
766 
774 void JSNIThrowRangeErrorException(JSNIEnv* env, const char* errmsg);
775 
784 
792 bool JSNIHasException(JSNIEnv* env);
793 
800 void JSNIClearException(JSNIEnv* env);
801 
802 #if defined(__cplusplus)
803 }
804 #endif
805 
809 struct _JSNIEnv {
811  void* reserved;
812 };
813 
814 // JSNI Versions.
819 #define JSNI_VERSION_1_0 0x00010000
820 
825 #define JSNI_VERSION_1_1 0x00010001
826 
827 
834 #define JSNI_VERSION_2_0 0x00020000
835 
839 #define JSNI_VERSION_2_1 0x00020001
840 
844 #define JSNI_VERSION_2_2 0x00020002
845 
846 #if defined(__cplusplus)
847 extern "C" {
848 #endif
849 
852 int JSNIInit(JSNIEnv* env, JSValueRef exports);
853 
854 #if defined(__cplusplus)
855 }
856 #endif
857 
858 #endif // INCLUDE_JSNI_H_
859 
void(* JSNICallback)(JSNIEnv *, const JSNICallbackInfo)
Callback helper type.
Definition: jsni.h:68
int JSNIInternalFieldCount(JSNIEnv *env, JSValueRef object)
Gets the number of the internal field fo a JavaScript object.
void * JSNIGetInternalField(JSNIEnv *env, JSValueRef object, int index)
Gets an internal field of a JavaScript object.
JSNICallback getter
Definition: jsni.h:126
JSValueRef JSNINewSymbol(JSNIEnv *env, JSValueRef val)
Constructs a new Symbol JavaScript value.
bool JSNIIsEmpty(JSNIEnv *env, JSValueRef val)
Tests whether a JavaScript value is empty.
Definition: jsni.h:112
size_t JSNIGetStringUtf8Length(JSNIEnv *env, JSValueRef string)
Returns the length in bytes of the UTF-8 representation of a string.
Definition: jsni.h:136
bool JSNIIsTypedArray(JSNIEnv *env, JSValueRef val)
Tests whether a JavaScript value is TypedArray.
JSNIAccessorPropertyDescriptor * accessor_attributes
Definition: jsni.h:140
Definition: jsni.h:96
JSNI environment structure.
Definition: jsni.h:809
bool JSNIHasException(JSNIEnv *env)
Tests whether a JavaScript exception is being thrown. It&#39;s different with error get from JSNIGetLastE...
JsTypedArrayType JSNIGetTypedArrayType(JSNIEnv *env, JSValueRef typed_array)
Returns the type of the JavaScript TypedArray value.
struct _JSNICallbackInfo * JSNICallbackInfo
Callback helper type.
Definition: jsni.h:63
JSValueRef JSNINewNumber(JSNIEnv *env, double val)
Constructs a new Number JavaScript value.
bool JSNIIsObject(JSNIEnv *env, JSValueRef val)
Tests whether a JavaScript value is a JavaScript object.
bool JSNIIsSymbol(JSNIEnv *env, JSValueRef val)
Tests whether the JavaScript value is Symbol.
JSNIErrorCode
Definition: jsni.h:144
bool JSNIHasProperty(JSNIEnv *env, JSValueRef object, const char *name)
Tests whether a JavaScript object has a property named name.
JSValueRef JSNIGetProperty(JSNIEnv *env, JSValueRef object, const char *name)
Returns the property of the JavaScript object.
void * data
Definition: jsni.h:132
int JSNIInit(JSNIEnv *env, JSValueRef exports)
This function is called by JSNI, not part of JSNI.
Definition: jsni.h:100
double JSNIToCDouble(JSNIEnv *env, JSValueRef val)
Converts the JavaScript value to C double.
Definition: jsni.h:80
Definition: jsni.h:92
size_t JSNIAcquireGlobalValue(JSNIEnv *env, JSGlobalValueRef val)
Acquire the val means to increase the reference count of the val. Once the reference count of the val...
void JSNISetGCCallback(JSNIEnv *env, JSGlobalValueRef val, void *args, JSNIGCCallback callback)
Sets a callback which will be called when the JavaScript value pointed by val is freed. The developer can pass an argument to callback by args. JSNISetGCCallback() is only valid when reference count is bigger than zero.
const char * msg
Definition: jsni.h:154
JSValueRef JSNINewStringFromUtf8(JSNIEnv *env, const char *src, size_t length)
Constructs a new String value from an array of characters in UTF-8 encoding.
Definition: jsni.h:98
bool JSNIIsBoolean(JSNIEnv *env, JSValueRef val)
Tests whether the JavaScript value is Boolean.
void JSNISetArrayElement(JSNIEnv *env, JSValueRef array, size_t index, JSValueRef value)
Sets an element of a JavaScript array.
JSValueRef value
Definition: jsni.h:118
void JSNISetReturnValue(JSNIEnv *env, JSNICallbackInfo info, JSValueRef val)
Sets the JavaScript return value for the callback.
bool JSNIIsNumber(JSNIEnv *env, JSValueRef val)
Tests whether the JavaScript value is Number.
struct _JSGlobalValueRef * JSGlobalValueRef
Global reference type.
Definition: jsni.h:73
void JSNIPushEscapableLocalScope(JSNIEnv *env)
Creates a local reference scope, and then all local references will be allocated within this referenc...
Definition: jsni.h:82
void JSNIDeleteGlobalValue(JSNIEnv *env, JSGlobalValueRef val)
Deletes the global reference pointed by val.
JSNIPropertyAttributes attributes
Definition: jsni.h:120
size_t JSNIGetTypedArrayLength(JSNIEnv *env, JSValueRef typed_array)
Returns the number of elements in the TypedArray value.
bool JSNIIsString(JSNIEnv *env, JSValueRef val)
Tests whether the JavaScript value is String.
JSValueRef JSNIGetPrototype(JSNIEnv *env, JSValueRef object)
Returns a prototype of a JavaScript object.
bool JSNIDefineProperty(JSNIEnv *env, JSValueRef object, const char *name, const JSNIPropertyDescriptor descriptor)
Defines a new property directly on an object, or modifies an existing property on an object...
uint32_t JSNIToUint32(JSNIEnv *env, JSValueRef val)
Converts the JavaScript value to uint32.
void JSNIThrowTypeErrorException(JSNIEnv *env, const char *errmsg)
Constructs an type error object with the message specified by errmsg and causes that type error to be...
Definition: jsni.h:152
struct _JSValueRef * JSValueRef
Reference type.
Definition: jsni.h:48
JSNIErrorInfo JSNIGetLastErrorInfo(JSNIEnv *env)
Tests whether there is error occured during pervious JSNI call. After calling JSNIGetLastErrorInfo()...
Definition: jsni.h:88
bool JSNIIsFunction(JSNIEnv *env, JSValueRef val)
Tests whether a JavaScript value is Function.
int32_t JSNIToInt32(JSNIEnv *env, JSValueRef val)
Converts the JavaScript value to int32.
void JSNIPushLocalScope(JSNIEnv *env)
Creates a local reference scope, and then all local references will be allocated within this referenc...
JSValueRef JSNINewObject(JSNIEnv *env)
Constructs a JavaScript object.
void * JSNIGetTypedArrayData(JSNIEnv *env, JSValueRef typed_array)
Returns the pointer to the buffer of TypedArray data.
bool JSNIToCBool(JSNIEnv *env, JSValueRef val)
Converts the JavaScript value to C bool.
Definition: jsni.h:90
bool JSNIDeleteProperty(JSNIEnv *env, JSValueRef object, const char *name)
Deletes the property of a JavaScript object.
bool JSNIIsArray(JSNIEnv *env, JSValueRef val)
Tests whether a JavaScript value is Array.
JSValueRef JSNINewTypedArray(JSNIEnv *env, JsTypedArrayType type, void *data, size_t length)
Constructs a JavaScript TypedArray object.
JSNIDataPropertyDescriptor * data_attributes
Definition: jsni.h:138
Definition: jsni.h:124
JSValueRef JSNIGetArgOfCallback(JSNIEnv *env, JSNICallbackInfo info, int id)
Returns the argument for the callback.
JSValueRef JSNINewString(JSNIEnv *env, const uint16_t *src, size_t length)
Constructs a new String value from an array of characters in two bytes.
JSValueRef JSNINewObjectWithInternalField(JSNIEnv *env, int count)
Constructs a JavaScript object with internal field. Internal field is a raw C pointer which can not b...
JSNIPropertyAttributes
Definition: jsni.h:104
bool JSNISetProperty(JSNIEnv *env, JSValueRef object, const char *name, JSValueRef property)
Sets a property of a JavaScript object.
JSValueRef JSNINewFunction(JSNIEnv *env, JSNICallback callback)
Constructs a JavaScript function with callback.
Definition: jsni.h:146
Definition: jsni.h:108
JSValueRef JSNIGetArrayElement(JSNIEnv *env, JSValueRef array, size_t index)
Returns an element of a JavaScript array.
bool JSNIIsUndefined(JSNIEnv *env, JSValueRef val)
Tests whether the JavaScript value is undefined.
Definition: jsni.h:84
void * reserved
Definition: jsni.h:811
void JSNIThrowRangeErrorException(JSNIEnv *env, const char *errmsg)
Constructs an range error object with the message specified by errmsg and causes that type error to b...
JSNIPropertyAttributes attributes
Definition: jsni.h:130
Definition: jsni.h:106
JSValueRef JSNIPopEscapableLocalScope(JSNIEnv *env, JSValueRef val)
Pops off the current local reference scope, frees all the local references in the local reference sco...
JSNICallback setter
Definition: jsni.h:128
Definition: jsni.h:116
Definition: jsni.h:148
size_t JSNIGetStringUtf8Chars(JSNIEnv *env, JSValueRef string, char *copy, size_t length)
Copyies a JavaScript string into a UTF-8 encoding string buffer.
JsTypedArrayType
The type of a typed JavaScript array.
Definition: jsni.h:78
void JSNISetInternalField(JSNIEnv *env, JSValueRef object, int index, void *field)
Sets an internal field of a JavaScript object.
int JSNIGetArgsLengthOfCallback(JSNIEnv *env, JSNICallbackInfo info)
Returns the number of arguments for the callback.
int64_t JSNIToInt64(JSNIEnv *env, JSValueRef val)
Converts the JavaScript value to int64.
Definition: jsni.h:94
void JSNIPopLocalScope(JSNIEnv *env)
Pops off the current local reference scope, frees all the local references in the local reference sco...
bool JSNIRegisterMethod(JSNIEnv *env, const JSValueRef recv, const char *name, JSNICallback callback)
Registers a native callback function.
void(* JSNIGCCallback)(JSNIEnv *, void *)
GCCallback helper type.
Definition: jsni.h:58
size_t JSNIGetArrayLength(JSNIEnv *env, JSValueRef array)
Returns the number of elements in the array.
int JSNIGetVersion(JSNIEnv *env)
Returns the version of the JSNI.
JSValueRef JSNIGetGlobalValue(JSNIEnv *env, JSGlobalValueRef val)
Returns a local JSValueRef value from a JSGlobalValueRef value.
void * JSNIGetDataOfCallback(JSNIEnv *env, JSNICallbackInfo info)
Get the raw data which is passed to this callback.
Definition: jsni.h:110
JSValueRef JSNINewBoolean(JSNIEnv *env, bool val)
Constructs a new Boolean JavaScript value.
size_t JSNIReleaseGlobalValue(JSNIEnv *env, JSGlobalValueRef val)
Release the val means to decrease the reference count of the val. Once the reference count of the val...
void JSNIThrowErrorException(JSNIEnv *env, const char *errmsg)
Constructs an error object with the message specified by errmsg and causes that error to be thrown...
void JSNIClearException(JSNIEnv *env)
Clear a JavaScript exception that is being thrown. If there is no exception, this routine has no effe...
JSValueRef JSNINewNull(JSNIEnv *env)
Constructs a new Null JavaScript value.
bool JSNIIsNull(JSNIEnv *env, JSValueRef val)
Tests whether the JavaScript value is Null.
JSValueRef JSNIGetThisOfCallback(JSNIEnv *env, JSNICallbackInfo info)
Returns "this" JavaScript object of the JavaScript function which current callback associated with...
JSValueRef JSNINewUndefined(JSNIEnv *env)
Constructs a new Undefined JavaScript value.
JSValueRef JSNINewArray(JSNIEnv *env, size_t initial_length)
Constructs a JavaScript array with initial length: initial_length.
JSNIErrorCode error_code
Definition: jsni.h:156
JSGlobalValueRef JSNINewGlobalValue(JSNIEnv *env, JSValueRef val)
Creates a new global reference to the JavaScript value referred to by the val argument. The global value must be explicitly disposed of by calling JSNIDeleteGlobalValue() or JSNIReleaseGlobalValue(). The global value will be alive untile calling JSNIDeleteGlobalValue() or JSNIReleaseGlobalValue() to dispose it. JSNIDeleteGlobalValue() dispose it immediately, whereas JSNIReleaseGlobalValue() dispose it when decrease the reference count to zero.
JSValueRef JSNICallFunction(JSNIEnv *env, JSValueRef func, JSValueRef recv, int argc, JSValueRef *argv)
Calls a JavaScript function.