Loading...
Searching...
No Matches
mjs_object_public.h
1/*
2 * Copyright (c) 2016 Cesanta Software Limited
3 * All rights reserved
4 */
5
6#ifndef MJS_OBJECT_PUBLIC_H_
7#define MJS_OBJECT_PUBLIC_H_
8
9#include <stddef.h>
10#include "mjs_core_public.h"
11#include "mjs_ffi_public.h"
12
13#if defined(__cplusplus)
14extern "C" {
15#endif /* __cplusplus */
16
17/*
18 * Returns true if the given value is an object or array.
19 */
20int mjs_is_object(mjs_val_t v);
21
22/*
23 * Returns true if the given value type is object-based (object, array, dataview).
24 */
25int mjs_is_object_based(mjs_val_t v);
26
27/* Make an empty object */
28mjs_val_t mjs_mk_object(struct mjs* mjs);
29
30/* Field types for struct-object conversion. */
31enum mjs_struct_field_type {
32 MJS_STRUCT_FIELD_TYPE_INVALID,
33 MJS_STRUCT_FIELD_TYPE_STRUCT, /* Struct, arg points to def. */
34 MJS_STRUCT_FIELD_TYPE_STRUCT_PTR, /* Ptr to struct, arg points to def. */
35 MJS_STRUCT_FIELD_TYPE_INT,
36 MJS_STRUCT_FIELD_TYPE_BOOL,
37 MJS_STRUCT_FIELD_TYPE_DOUBLE,
38 MJS_STRUCT_FIELD_TYPE_FLOAT,
39 MJS_STRUCT_FIELD_TYPE_CHAR_PTR, /* NUL-terminated string. */
40 MJS_STRUCT_FIELD_TYPE_VOID_PTR, /* Converted to foreign ptr. */
41 MJS_STRUCT_FIELD_TYPE_MG_STR_PTR, /* Converted to string. */
42 MJS_STRUCT_FIELD_TYPE_MG_STR, /* Converted to string. */
43 MJS_STRUCT_FIELD_TYPE_DATA, /* Data, arg is length, becomes string. */
44 MJS_STRUCT_FIELD_TYPE_INT8,
45 MJS_STRUCT_FIELD_TYPE_INT16,
46 MJS_STRUCT_FIELD_TYPE_UINT8,
47 MJS_STRUCT_FIELD_TYPE_UINT16,
48 /*
49 * User-provided function. Arg is a pointer to function that takes void *
50 * (pointer to field within the struct) and returns mjs_val_t:
51 * mjs_val_t field_value(struct mjs *mjs, const void *field_ptr) { ... }
52 */
53 MJS_STRUCT_FIELD_TYPE_CUSTOM,
54};
55
56/* C structure layout descriptor - needed by mjs_struct_to_obj */
58 const char* name;
59 int offset;
60 enum mjs_struct_field_type type;
61 const void* arg; /* Additional argument, used for some types. */
62};
63
64/* Create flat JS object from a C memory descriptor */
65mjs_val_t
66 mjs_struct_to_obj(struct mjs* mjs, const void* base, const struct mjs_c_struct_member* members);
67
68/*
69 * Lookup property `name` in object `obj`. If `obj` holds no such property,
70 * an `undefined` value is returned.
71 *
72 * If `name_len` is ~0, `name` is assumed to be NUL-terminated and
73 * `strlen(name)` is used.
74 */
75mjs_val_t mjs_get(struct mjs* mjs, mjs_val_t obj, const char* name, size_t name_len);
76
77/*
78 * Like mjs_get but with a JS string.
79 */
80mjs_val_t mjs_get_v(struct mjs* mjs, mjs_val_t obj, mjs_val_t name);
81
82/*
83 * Like mjs_get_v but lookup the prototype chain.
84 */
85mjs_val_t mjs_get_v_proto(struct mjs* mjs, mjs_val_t obj, mjs_val_t key);
86
87/*
88 * Set object property. Behaves just like JavaScript assignment.
89 */
90mjs_err_t mjs_set(struct mjs* mjs, mjs_val_t obj, const char* name, size_t len, mjs_val_t val);
91
92/*
93 * Like mjs_set but the name is already a JS string.
94 */
95mjs_err_t mjs_set_v(struct mjs* mjs, mjs_val_t obj, mjs_val_t name, mjs_val_t val);
96
97/*
98 * Delete own property `name` of the object `obj`. Does not follow the
99 * prototype chain.
100 *
101 * If `name_len` is ~0, `name` is assumed to be NUL-terminated and
102 * `strlen(name)` is used.
103 *
104 * Returns 0 on success, -1 on error.
105 */
106int mjs_del(struct mjs* mjs, mjs_val_t obj, const char* name, size_t len);
107
108/*
109 * Iterate over `obj` properties.
110 * First call should set `iterator` to MJS_UNDEFINED.
111 * Return object's key (a string), or MJS_UNDEFINED when no more keys left.
112 * Do not mutate the object during iteration.
113 *
114 * Example:
115 * mjs_val_t key, iter = MJS_UNDEFINED;
116 * while ((key = mjs_next(mjs, obj, &iter)) != MJS_UNDEFINED) {
117 * // Do something with the obj/key ...
118 * }
119 */
120mjs_val_t mjs_next(struct mjs* mjs, mjs_val_t obj, mjs_val_t* iterator);
121
122typedef void (*mjs_custom_obj_destructor_t)(struct mjs* mjs, mjs_val_t object);
123
124/*
125 * Destructor property name. If set, must be a foreign pointer to a function
126 * that will be called just before the object is freed.
127 */
128#define MJS_DESTRUCTOR_PROP_NAME "__d"
129
130#if defined(__cplusplus)
131}
132#endif /* __cplusplus */
133
134#endif /* MJS_OBJECT_PUBLIC_H_ */
Definition mjs_object_public.h:57
Definition mjs_core.h:63