node-addon-layer
C API For writing Node modules
 All Data Structures Functions Variables Typedefs Enumerations Enumerator Groups Pages
shim.h
1 /*
2  * Copyright Joyent, Inc. and other Node contributors.
3  *
4  * Permission is hereby granted, free of charge, to any person obtaining a
5  * copy of this software and associated documentation files (the
6  * "Software"), to deal in the Software without restriction, including
7  * without limitation the rights to use, copy, modify, merge, publish,
8  * distribute, sublicense, and/or sell copies of the Software, and to permit
9  * persons to whom the Software is furnished to do so, subject to the
10  * following conditions:
11  *
12  * The above copyright notice and this permission notice shall be included
13  * in all copies or substantial portions of the Software.
14  *
15  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
16  * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
17  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN
18  * NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
19  * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
20  * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
21  * USE OR OTHER DEALINGS IN THE SOFTWARE.
22  */
23 
24 #ifndef NODE_SHIM_H
25 #define NODE_SHIM_H
26 
27 #ifdef __cplusplus
28 extern "C" {
29 #endif
30 
31 #include "stdint.h"
32 #include "node_version.h"
33 
40 #define SHIM_ABI_VERSION 1
41 
43 typedef enum shim_type {
58 } shim_type_t;
59 
65 typedef struct shim_val_s shim_val_t;
66 
68 typedef struct shim_ctx_s shim_ctx_t;
69 
71 typedef struct shim_args_s shim_args_t;
72 
74 typedef int shim_bool_t;
75 
77 typedef void (* node_register_func)(void*, void*);
79 void shim_module_initialize(void*, void*);
80 
82 typedef int (* register_func)(shim_ctx_t*, shim_val_t*, shim_val_t*);
85 
91  int version;
92  void* handle;
93  const char* fname;
95  const char* mname;
96 };
97 
98 
99 /* Sigh, NODE_MODULE_VERSION isn't in node_version.h before 0.12 */
100 #ifndef NODE_MODULE_VERSION
101 # if NODE_VERSION_AT_LEAST(0, 11, 0)
102 # define NODE_MODULE_VERSION 0x000C
103 # elif NODE_VERSION_AT_LEAST(0, 10, 0)
104 # define NODE_MODULE_VERSION 0x000B
105 # elif NODE_VERSION_AT_LEAST(0, 8, 0)
106 # define NODE_MODULE_VERSION 1
107 # else
108 # error "The shim requires at least node v0.8.0"
109 # endif /* checking versions */
110 #endif /* checking NODE_MODULE_VERSION defined */
111 
112 
117 #define SHIM_MODULE(name, func) \
118 register_func shim_initialize = &func; \
119 struct shim_module_struct name ## _module = { \
120  NODE_MODULE_VERSION, \
121  NULL, \
122  __FILE__, \
123  (node_register_func)&shim_module_initialize, \
124  #name, \
125 };
126 
128 typedef int (* shim_func)(shim_ctx_t*, shim_args_t*);
129 
134 typedef struct shim_fspec_s {
135  const char* name;
137  uint16_t nargs;
138  void* data;
139  uint32_t flags;
140  uint32_t reserved;
141 } shim_fspec_t;
142 
143 
145 #define SHIM_FS_FULL(name, cfunc, nargs, data, flags) \
146  { name, &cfunc, nargs, data, flags, 0 }
147 
148 #define SHIM_FS_DEF(cfunc, nargs, data) \
149  SHIM_FS_FULL(#cfunc, cfunc, nargs, data, 0)
150 
151 #define SHIM_FS(cfunc) \
152  SHIM_FS_DEF(cfunc, 0, NULL)
153 
154 #define SHIM_FS_END \
155  { NULL, NULL, 0, NULL, 0, 0 }
156 
166 shim_bool_t shim_value_is(shim_val_t* val, shim_type_t type);
168 shim_bool_t shim_value_to(shim_ctx_t* ctx, shim_val_t* val, shim_type_t type,
169  shim_val_t* rval);
170 
171 
173 void shim_value_release(shim_val_t* val);
174 
176 shim_val_t* shim_undefined();
178 shim_val_t* shim_null();
179 
180 const char* shim_type_str(shim_type_t type);
191 shim_val_t* shim_obj_new(shim_ctx_t* context, shim_val_t* klass,
192  shim_val_t* proto);
194 shim_val_t* shim_obj_new_instance(shim_ctx_t* context, shim_val_t* klass,
195  size_t argc, shim_val_t** argv);
196 
198 shim_val_t* shim_obj_clone(shim_ctx_t* ctx, shim_val_t* src);
199 
201 shim_bool_t shim_obj_has_name(shim_ctx_t* ctx, shim_val_t* obj,
202  const char* name);
204 shim_bool_t shim_obj_has_id(shim_ctx_t* ctx, shim_val_t* obj,
205  uint32_t id);
207 shim_bool_t shim_obj_has_sym(shim_ctx_t* ctx, shim_val_t* obj, shim_val_t* sym);
208 
210 shim_bool_t shim_obj_set_prop_name(shim_ctx_t* ctx, shim_val_t* recv,
211  const char* name, shim_val_t* val);
213 shim_bool_t shim_obj_set_prop_id(shim_ctx_t* ctx, shim_val_t* recv,
214  uint32_t id, shim_val_t* val);
216 shim_bool_t shim_obj_set_prop_sym(shim_ctx_t* ctx, shim_val_t* recv,
217  shim_val_t* sym, shim_val_t* val);
219 shim_bool_t shim_obj_set_private(shim_ctx_t* ctx, shim_val_t* obj, void* data);
221 shim_bool_t shim_obj_set_funcs(shim_ctx_t* ctx, shim_val_t* recv,
222  const shim_fspec_t* funcs);
223 
225 shim_bool_t shim_obj_get_prop_name(shim_ctx_t* ctx, shim_val_t* obj,
226  const char* name, shim_val_t* rval);
228 shim_bool_t shim_obj_get_prop_id(shim_ctx_t* ctx, shim_val_t* obj,
229  uint32_t id, shim_val_t* rval);
231 shim_bool_t shim_obj_get_prop_sym(shim_ctx_t* ctx, shim_val_t* obj,
232  shim_val_t* sym, shim_val_t* rval);
234 shim_bool_t shim_obj_get_private(shim_ctx_t* ctx, shim_val_t* obj, void** data);
235 
246 shim_val_t* shim_persistent_new(shim_ctx_t* ctx, shim_val_t* val);
248 void shim_persistent_dispose(shim_val_t* val);
249 
250 
252 typedef void (* shim_weak_cb)(shim_val_t*, void*);
254 void shim_obj_make_weak(shim_ctx_t* ctx, shim_val_t* val, void* data,
255  shim_weak_cb cb);
257 void shim_obj_clear_weak(shim_val_t* val);
258 
269 shim_val_t* shim_func_new(shim_ctx_t* ctx, shim_func cfunc, size_t argc,
270  int32_t flags, const char* name, void* data);
271 
272 
274 shim_bool_t shim_func_call_sym(shim_ctx_t* ctx, shim_val_t* self,
275  shim_val_t* name, size_t argc, shim_val_t** argv, shim_val_t* rval);
277 shim_bool_t shim_func_call_name(shim_ctx_t* ctx, shim_val_t* self,
278  const char* name, size_t argc, shim_val_t** argv, shim_val_t* rval);
280 shim_bool_t shim_func_call_val(shim_ctx_t* ctx, shim_val_t* self,
281  shim_val_t* func, size_t argc, shim_val_t** argv, shim_val_t* rval);
282 
283 
285 shim_bool_t shim_make_callback_sym(shim_ctx_t* ctx, shim_val_t* self,
286  shim_val_t* sym, size_t argc, shim_val_t** argv, shim_val_t* rval);
288 shim_bool_t shim_make_callback_val(shim_ctx_t* ctx, shim_val_t* self,
289  shim_val_t* fval, size_t argc, shim_val_t** argv, shim_val_t* rval);
291 shim_bool_t shim_make_callback_name(shim_ctx_t* ctx, shim_val_t* obj,
292  const char* name, size_t argc, shim_val_t** argv, shim_val_t* rval);
293 
303 shim_val_t* shim_number_new(shim_ctx_t* ctx, double d);
305 double shim_number_value(shim_val_t* obj);
306 
308 shim_val_t* shim_integer_new(shim_ctx_t* ctx, int32_t i);
310 shim_val_t* shim_integer_uint(shim_ctx_t* ctx, uint32_t i);
312 int64_t shim_integer_value(shim_val_t* val);
314 int32_t shim_integer_int32_value(shim_val_t* val);
316 uint32_t shim_integer_uint32_value(shim_val_t* val);
317 
327 shim_val_t* shim_string_new(shim_ctx_t* ctx);
329 shim_val_t* shim_string_new_copy(shim_ctx_t* ctx, const char* data);
331 shim_val_t* shim_string_new_copyn(shim_ctx_t* ctx, const char* data,
332  size_t len);
333 
335 size_t shim_string_length(shim_val_t* val);
337 size_t shim_string_length_utf8(shim_val_t* val);
338 
340 const char* shim_string_value(shim_val_t* val);
341 /* TODO enum for options */
343 size_t shim_string_write_ascii(shim_val_t* val, char* buff, size_t start,
344  size_t len, int32_t options);
345 
355 shim_val_t* shim_array_new(shim_ctx_t* ctx, size_t len);
357 size_t shim_array_length(shim_val_t* arr);
359 shim_bool_t shim_array_get(shim_ctx_t* ctx, shim_val_t* arr, int32_t idx,
360  shim_val_t* rval);
362 shim_bool_t shim_array_set(shim_ctx_t* ctx, shim_val_t* arr, int32_t idx,
363  shim_val_t* val);
364 
374 typedef void (* shim_buffer_free)(char*, void*);
376 shim_val_t* shim_buffer_new(shim_ctx_t*, size_t);
378 shim_val_t* shim_buffer_new_copy(shim_ctx_t*, const char*, size_t);
380 shim_val_t* shim_buffer_new_external(shim_ctx_t*, char*, size_t,
381  shim_buffer_free, void*);
382 
384 char* shim_buffer_value(shim_val_t*);
386 size_t shim_buffer_length(shim_val_t*);
387 
397 shim_val_t* shim_external_new(shim_ctx_t* ctx, void* data);
399 void* shim_external_value(shim_ctx_t* ctx, shim_val_t* val);
400 
410 shim_val_t* shim_error_new(shim_ctx_t* ctx, const char* msg, ...);
412 shim_val_t* shim_error_type_new(shim_ctx_t* ctx, const char* msg, ...);
414 shim_val_t* shim_error_range_new(shim_ctx_t* ctx, const char* msg, ...);
415 
417 shim_bool_t shim_exception_pending(shim_ctx_t* ctx);
419 void shim_exception_clear(shim_ctx_t* ctx);
421 void shim_exception_set(shim_ctx_t* ctx, shim_val_t* val);
423 shim_bool_t shim_exception_get(shim_ctx_t* ctx, shim_val_t* rval);
424 
426 void shim_throw_error(shim_ctx_t* ctx, const char* msg, ...);
428 void shim_throw_type_error(shim_ctx_t* ctx, const char* msg, ...);
430 void shim_throw_range_error(shim_ctx_t* ctx, const char* msg, ...);
431 
441 shim_bool_t shim_unpack_one(shim_ctx_t* ctx, shim_args_t* args, uint32_t idx,
442  shim_type_t type, void* rval);
444 shim_bool_t shim_unpack_type(shim_ctx_t* ctx, shim_val_t* arg,
445  shim_type_t type, void* rval);
447 shim_bool_t shim_unpack(shim_ctx_t* ctx, shim_args_t* args, shim_type_t type,
448  ...);
450 int shim_unpack_fmt(shim_ctx_t* ctx, shim_args_t* args, const char* fmt, ...);
451 
453 size_t shim_args_length(shim_args_t* args);
455 shim_val_t* shim_args_get(shim_args_t*, size_t idx);
457 shim_bool_t shim_args_set_rval(shim_ctx_t* ctx, shim_args_t* args,
458  shim_val_t* val);
460 shim_val_t* shim_args_get_this(shim_ctx_t* ctx, shim_args_t* args);
462 void* shim_args_get_data(shim_ctx_t* ctx, shim_args_t* args);
463 
476 typedef struct shim_work_s shim_work_t;
477 
479 typedef void (* shim_work_cb)(shim_work_t*, void*);
481 typedef void (* shim_after_work)(shim_ctx_t*, shim_work_t*, int, void*);
483 void shim_queue_work(shim_work_cb, shim_after_work, void* hint);
484 
487 #ifndef TRUE
488 #define TRUE 1
489 #endif
490 
491 #ifndef FALSE
492 #define FALSE 0
493 #endif
494 
495 #ifndef offset_of
496 // g++ in strict mode complains loudly about the system offsetof() macro
497 // because it uses NULL as the base address.
498 # define offset_of(type, member) \
499  ((intptr_t) ((char *) &(((type *) 8)->member) - 8))
500 #endif
501 
502 #ifndef container_of
503 # define container_of(ptr, type, member) \
504  ((type *) ((char *) (ptr) - offset_of(type, member)))
505 #endif
506 
507 #ifdef __cplusplus
508 }
509 #endif
510 
511 #endif