Loading...
Searching...
No Matches
mjs_primitive_public.h
1/*
2 * Copyright (c) 2016 Cesanta Software Limited
3 * All rights reserved
4 */
5
6#ifndef MJS_PRIMITIVE_PUBLIC_H_
7#define MJS_PRIMITIVE_PUBLIC_H_
8
9#include "mjs_core_public.h"
10
11#if defined(__cplusplus)
12extern "C" {
13#endif /* __cplusplus */
14
15/* JavaScript `null` value */
16#define MJS_NULL MJS_TAG_NULL
17
18/* JavaScript `undefined` value */
19#define MJS_UNDEFINED MJS_TAG_UNDEFINED
20
21#define MJS_MK_FN(fn) mjs_mk_foreign_func(mjs, (mjs_func_ptr_t)fn)
22
23/* Function pointer type used in `mjs_mk_foreign_func`. */
24typedef void (*mjs_func_ptr_t)(void);
25
26/*
27 * Make `null` primitive value.
28 *
29 * NOTE: this function is deprecated and will be removed in future releases.
30 * Use `MJS_NULL` instead.
31 */
32mjs_val_t mjs_mk_null(void);
33
34/* Returns true if given value is a primitive `null` value */
35int mjs_is_null(mjs_val_t v);
36
37/*
38 * Make `undefined` primitive value.
39 *
40 * NOTE: this function is deprecated and will be removed in future releases.
41 * Use `MJS_UNDEFINED` instead.
42 */
43mjs_val_t mjs_mk_undefined(void);
44
45/* Returns true if given value is a primitive `undefined` value */
46int mjs_is_undefined(mjs_val_t v);
47
48/* Make numeric primitive value */
49mjs_val_t mjs_mk_number(struct mjs* mjs, double num);
50
51/*
52 * Returns number value stored in `mjs_val_t` as `double`.
53 *
54 * Returns NaN for non-numbers.
55 */
56double mjs_get_double(struct mjs* mjs, mjs_val_t v);
57
58/*
59 * Returns number value stored in `mjs_val_t` as `int`. If the number value is
60 * not an integer, the fraction part will be discarded.
61 *
62 * If the given value is a non-number, or NaN, the result is undefined.
63 */
64int mjs_get_int(struct mjs* mjs, mjs_val_t v);
65
66/*
67 * Like mjs_get_int but ensures that the returned type
68 * is a 32-bit signed integer.
69 */
70int32_t mjs_get_int32(struct mjs* mjs, mjs_val_t v);
71
72/* Returns true if given value is a primitive number value */
73int mjs_is_number(mjs_val_t v);
74
75/*
76 * Make JavaScript value that holds C/C++ `void *` pointer.
77 *
78 * A foreign value is completely opaque and JS code cannot do anything useful
79 * with it except holding it in properties and passing it around.
80 * It behaves like a sealed object with no properties.
81 *
82 * NOTE:
83 * Only valid pointers (as defined by each supported architecture) will fully
84 * preserved. In particular, all supported 64-bit architectures (x86_64, ARM-64)
85 * actually define a 48-bit virtual address space.
86 * Foreign values will be sign-extended as required, i.e creating a foreign
87 * value of something like `(void *) -1` will work as expected. This is
88 * important because in some 64-bit OSs (e.g. Solaris) the user stack grows
89 * downwards from the end of the address space.
90 *
91 * If you need to store exactly sizeof(void*) bytes of raw data where
92 * `sizeof(void*)` >= 8, please use byte arrays instead.
93 */
94mjs_val_t mjs_mk_foreign(struct mjs* mjs, void* ptr);
95
96/*
97 * Make JavaScript value that holds C/C++ function pointer, similarly to
98 * `mjs_mk_foreign`.
99 */
100mjs_val_t mjs_mk_foreign_func(struct mjs* mjs, mjs_func_ptr_t fn);
101
102/*
103 * Returns `void *` pointer stored in `mjs_val_t`.
104 *
105 * Returns NULL if the value is not a foreign pointer.
106 */
107void* mjs_get_ptr(struct mjs* mjs, mjs_val_t v);
108
109/* Returns true if given value holds `void *` pointer */
110int mjs_is_foreign(mjs_val_t v);
111
112mjs_val_t mjs_mk_boolean(struct mjs* mjs, int v);
113int mjs_get_bool(struct mjs* mjs, mjs_val_t v);
114int mjs_is_boolean(mjs_val_t v);
115
116mjs_val_t mjs_mk_function(struct mjs* mjs, size_t off);
117int mjs_is_function(mjs_val_t v);
118
119#if defined(__cplusplus)
120}
121#endif /* __cplusplus */
122
123#endif /* MJS_PRIMITIVE_PUBLIC_H_ */
Definition mjs_core.h:63