|
| 1 | +#include "object_ref.h" |
| 2 | +#include "js_native_api.h" |
| 3 | +#include "node_api_util.h" |
| 4 | +#include "type_conv.h" |
| 5 | +#include "util.h" |
| 6 | + |
| 7 | +#import <Foundation/Foundation.h> |
| 8 | + |
| 9 | +namespace objc_bridge { |
| 10 | + |
| 11 | +void ObjectRef_finalize(napi_env env, void *data, void *hint) { free(data); } |
| 12 | + |
| 13 | +NAPI_FUNCTION(ObjectRef_constructor) { |
| 14 | + napi_value jsThis, arg; |
| 15 | + size_t argc = 1; |
| 16 | + napi_get_cb_info(env, cbinfo, &argc, &arg, &jsThis, nullptr); |
| 17 | + |
| 18 | + auto data = malloc(sizeof(id)); |
| 19 | + napi_wrap(env, jsThis, data, ObjectRef_finalize, nullptr, nullptr); |
| 20 | + |
| 21 | + napi_valuetype argType; |
| 22 | + napi_typeof(env, arg, &argType); |
| 23 | + |
| 24 | + if (argType != napi_undefined && argType != napi_null) { |
| 25 | + const char *argenc = "@"; |
| 26 | + auto conv = TypeConv::Make(env, &argenc); |
| 27 | + bool shouldFree; |
| 28 | + conv->toNative(env, arg, data, &shouldFree, &shouldFree); |
| 29 | + } else { |
| 30 | + *(id *)data = nil; |
| 31 | + } |
| 32 | + |
| 33 | + return jsThis; |
| 34 | +} |
| 35 | + |
| 36 | +NAPI_FUNCTION(ObjectRef_unwrap) { |
| 37 | + napi_value jsThis; |
| 38 | + napi_get_cb_info(env, cbinfo, nullptr, nullptr, &jsThis, nullptr); |
| 39 | + |
| 40 | + void *data; |
| 41 | + napi_unwrap(env, jsThis, &data); |
| 42 | + |
| 43 | + id obj = *(id *)data; |
| 44 | + if (obj == nil) { |
| 45 | + napi_throw_error(env, nullptr, "ObjectRef.unwrap returned nil"); |
| 46 | + return nullptr; |
| 47 | + } |
| 48 | + |
| 49 | + const char *argenc = "@"; |
| 50 | + auto conv = TypeConv::Make(env, &argenc); |
| 51 | + return conv->toJS(env, &obj); |
| 52 | +} |
| 53 | + |
| 54 | +NAPI_FUNCTION(ObjectRef_get_value) { |
| 55 | + napi_value jsThis; |
| 56 | + napi_get_cb_info(env, cbinfo, nullptr, nullptr, &jsThis, nullptr); |
| 57 | + |
| 58 | + void *data; |
| 59 | + napi_unwrap(env, jsThis, &data); |
| 60 | + |
| 61 | + id obj = *(id *)data; |
| 62 | + const char *argenc = "@"; |
| 63 | + auto conv = TypeConv::Make(env, &argenc); |
| 64 | + return conv->toJS(env, &obj); |
| 65 | +} |
| 66 | + |
| 67 | +NAPI_FUNCTION(ObjectRef_set_value) { |
| 68 | + napi_value jsThis, arg; |
| 69 | + size_t argc = 1; |
| 70 | + napi_get_cb_info(env, cbinfo, &argc, &arg, &jsThis, nullptr); |
| 71 | + |
| 72 | + void *data; |
| 73 | + napi_unwrap(env, jsThis, &data); |
| 74 | + |
| 75 | + const char *argenc = "@"; |
| 76 | + auto conv = TypeConv::Make(env, &argenc); |
| 77 | + bool shouldFree; |
| 78 | + conv->toNative(env, arg, data, &shouldFree, &shouldFree); |
| 79 | + |
| 80 | + return nullptr; |
| 81 | +} |
| 82 | + |
| 83 | +NAPI_FUNCTION(ObjectRef_customInspect) { |
| 84 | + napi_value jsThis; |
| 85 | + napi_get_cb_info(env, cbinfo, nullptr, nullptr, &jsThis, nullptr); |
| 86 | + |
| 87 | + void *data; |
| 88 | + napi_unwrap(env, jsThis, &data); |
| 89 | + |
| 90 | + id obj = *(id *)data; |
| 91 | + |
| 92 | + std::string inspect = "ObjectRef("; |
| 93 | + if (obj == nil) { |
| 94 | + inspect += "nil"; |
| 95 | + } else { |
| 96 | + inspect += [[obj description] UTF8String]; |
| 97 | + } |
| 98 | + inspect += ")"; |
| 99 | +
|
| 100 | + napi_value result; |
| 101 | + napi_create_string_utf8(env, inspect.c_str(), NAPI_AUTO_LENGTH, &result); |
| 102 | + return result; |
| 103 | +} |
| 104 | +
|
| 105 | +napi_value defineObjectRefClass(napi_env env) { |
| 106 | + napi_value result; |
| 107 | + const napi_property_descriptor properties[] = { |
| 108 | + { |
| 109 | + .utf8name = "unwrap", |
| 110 | + .name = nullptr, |
| 111 | + .method = JS_ObjectRef_unwrap, |
| 112 | + .getter = nullptr, |
| 113 | + .setter = nullptr, |
| 114 | + .value = nullptr, |
| 115 | + .attributes = napi_default, |
| 116 | + .data = nullptr, |
| 117 | + }, |
| 118 | + { |
| 119 | + .utf8name = "value", |
| 120 | + .name = nullptr, |
| 121 | + .method = nullptr, |
| 122 | + .getter = JS_ObjectRef_get_value, |
| 123 | + .setter = JS_ObjectRef_set_value, |
| 124 | + .value = nullptr, |
| 125 | + .attributes = napi_default, |
| 126 | + .data = nullptr, |
| 127 | + }, |
| 128 | + { |
| 129 | + .utf8name = nullptr, |
| 130 | + .name = jsSymbolFor(env, "nodejs.util.inspect.custom"), |
| 131 | + .method = JS_ObjectRef_customInspect, |
| 132 | + .getter = nullptr, |
| 133 | + .setter = nullptr, |
| 134 | + .value = nullptr, |
| 135 | + .attributes = napi_default, |
| 136 | + .data = nullptr, |
| 137 | + }}; |
| 138 | + napi_define_class(env, "ObjectRef", NAPI_AUTO_LENGTH, |
| 139 | + JS_ObjectRef_constructor, nullptr, 3, properties, &result); |
| 140 | + return result; |
| 141 | +} |
| 142 | +
|
| 143 | +} // namespace objc_bridge |
0 commit comments