Skip to content

Commit 5dca383

Browse files
committed
few fixes
1 parent d4b7eca commit 5dca383

3 files changed

Lines changed: 61 additions & 60 deletions

File tree

binding.gyp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222
}],
2323
],
2424
"sources": [
25-
"js/addon2.cpp",
25+
"js/addon.cpp",
2626
"src/customlabels.cpp",
2727
"src/hashmap.c"
2828
],

js/addon.cpp

Lines changed: 58 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -12,15 +12,16 @@
1212
#include <stdlib.h>
1313

1414
extern "C" {
15-
using v8::Global;
16-
using v8::Object;
17-
__thread int custom_labels_als_identity_hash;
15+
using v8::Global;
16+
using v8::Object;
17+
__thread int custom_labels_als_identity_hash;
1818

19-
thread_local Global<Object> custom_labels_als_handle;
19+
thread_local Global<Object> custom_labels_als_handle;
2020
}
2121

2222
namespace custom_labels {
2323
using node::ObjectWrap;
24+
using v8::Context;
2425
using v8::Exception;
2526
using v8::Function;
2627
using v8::FunctionCallbackInfo;
@@ -33,8 +34,6 @@ using v8::Object;
3334
using v8::ObjectTemplate;
3435
using v8::String;
3536
using v8::Value;
36-
using v8::Context;
37-
3837

3938
#define hm custom_labels_async_hashmap
4039

@@ -52,9 +51,10 @@ class ClWrap : public ObjectWrap {
5251
public:
5352
~ClWrap() override;
5453
static void Init(Local<Object> exports);
54+
5555
private:
56-
static void New(const v8::FunctionCallbackInfo<v8::Value>& args);
57-
static void ToString(const v8::FunctionCallbackInfo<v8::Value>& args);
56+
static void New(const v8::FunctionCallbackInfo<v8::Value> &args);
57+
static void ToString(const v8::FunctionCallbackInfo<v8::Value> &args);
5858
custom_labels_labelset_t *underlying_;
5959
// Homemade RTTI. If the bytes at this address equal
6060
// CLWRAP_TOKEN_VALUE, the agent knows it's looking at
@@ -63,15 +63,12 @@ class ClWrap : public ObjectWrap {
6363
explicit ClWrap(custom_labels_labelset_t *underlying);
6464
};
6565

66-
ClWrap::~ClWrap() {
67-
custom_labels_free(underlying_);
68-
}
69-
70-
ClWrap::ClWrap(custom_labels_labelset_t *underlying) :
71-
underlying_(underlying),
72-
token_(CLWRAP_TOKEN_VALUE) {}
73-
74-
void ClWrap::New(const v8::FunctionCallbackInfo<v8::Value>& args) {
66+
ClWrap::~ClWrap() { custom_labels_free(underlying_); }
67+
68+
ClWrap::ClWrap(custom_labels_labelset_t *underlying)
69+
: underlying_(underlying), token_(CLWRAP_TOKEN_VALUE) {}
70+
71+
void ClWrap::New(const v8::FunctionCallbackInfo<v8::Value> &args) {
7572
Isolate *isolate = args.GetIsolate();
7673

7774
if (!args.IsConstructCall()) [[unlikely]] {
@@ -80,31 +77,35 @@ void ClWrap::New(const v8::FunctionCallbackInfo<v8::Value>& args) {
8077
}
8178
if (args.Length() % 2 == 0) {
8279
isolate->ThrowError("Must be called like `new ClWrap(old, (k, v)*)`");
83-
return;
80+
return;
8481
}
8582

8683
size_t new_labels = args.Length() / 2;
8784

88-
// args[0] is the old ls, args[n+1] is the nth key, args[n+2] is the nth value.
85+
// args[0] is the old ls, args[n+1] is the nth key, args[n+2] is the nth
86+
// value.
8987
custom_labels_labelset_t *old = NULL;
9088
if (!args[0]->IsUndefined()) {
9189
if (!args[0]->IsObject()) {
92-
isolate->ThrowError("First argument must be the old object or `undefined`");
90+
isolate->ThrowError(
91+
"First argument must be the old object or `undefined`");
9392
return;
9493
}
9594
ClWrap *old_wrap = ObjectWrap::Unwrap<ClWrap>(args[0].As<Object>());
9695
if (!old_wrap || old_wrap->token_ != CLWRAP_TOKEN_VALUE) {
9796
// TODO: Better way to do this?
9897
// https://stackoverflow.com/questions/8994196/how-to-check-for-correct-type-when-calling-objectwrapunwrap-in-a-nodejs-add-on
99-
isolate->ThrowError("First argument must be the old object or `undefined`");
98+
isolate->ThrowError(
99+
"First argument must be the old object or `undefined`");
100100
return;
101101
}
102102
old = old_wrap->underlying_;
103103
}
104104

105105
custom_labels_labelset_t *underlying;
106106
if (old) {
107-
underlying = custom_labels_clone_with_capacity(old, custom_labels_count(old) + new_labels);
107+
underlying = custom_labels_clone_with_capacity(
108+
old, custom_labels_count(old) + new_labels);
108109
if (!underlying) {
109110
isolate->ThrowError("allocation failed");
110111
return;
@@ -117,9 +118,8 @@ void ClWrap::New(const v8::FunctionCallbackInfo<v8::Value>& args) {
117118
}
118119
}
119120

120-
ClWrap *new_ = new ClWrap(underlying);
121+
ClWrap *new_ = new ClWrap(underlying);
121122
auto me = std::unique_ptr<ClWrap>(new_);
122-
123123

124124
for (size_t i = 0; i < new_labels; ++i) {
125125
int k_idx = 2 * i + 1;
@@ -138,82 +138,83 @@ void ClWrap::New(const v8::FunctionCallbackInfo<v8::Value>& args) {
138138
int v_len = v->Utf8Length(isolate);
139139
auto v_buf = std::make_unique<char[]>(v_len);
140140

141-
k->WriteUtf8(isolate, k_buf.get(), k_len, nullptr, String::NO_NULL_TERMINATION);
142-
v->WriteUtf8(isolate, v_buf.get(), v_len, nullptr, String::NO_NULL_TERMINATION);
141+
k->WriteUtf8(isolate, k_buf.get(), k_len, nullptr,
142+
String::NO_NULL_TERMINATION);
143+
v->WriteUtf8(isolate, v_buf.get(), v_len, nullptr,
144+
String::NO_NULL_TERMINATION);
143145

144-
custom_labels_string_t key { (size_t)k_len, (unsigned char *)k_buf.get() };
145-
custom_labels_string_t value { (size_t)v_len, (unsigned char *)v_buf.get() };
146+
custom_labels_string_t key{(size_t)k_len, (unsigned char *)k_buf.get()};
147+
custom_labels_string_t value{(size_t)v_len, (unsigned char *)v_buf.get()};
146148
int err = custom_labels_set(underlying, key, value, nullptr);
147149

148150
if (err) {
149151
// TODO - better error message here.
150-
isolate->ThrowError("Underlying custom_labels_set call failed: probably an allocation error.");
152+
isolate->ThrowError("Underlying custom_labels_set call failed: probably "
153+
"an allocation error.");
151154
return;
152155
}
153156
}
154157

155158
me.release()->Wrap(args.This());
156-
159+
157160
args.GetReturnValue().Set(args.This());
158161
}
159162

160-
void ClWrap::ToString(const v8::FunctionCallbackInfo<v8::Value>& args) {
161-
Isolate* isolate = args.GetIsolate();
162-
163-
ClWrap* obj = ObjectWrap::Unwrap<ClWrap>(args.This());
163+
void ClWrap::ToString(const v8::FunctionCallbackInfo<v8::Value> &args) {
164+
Isolate *isolate = args.GetIsolate();
165+
166+
ClWrap *obj = ObjectWrap::Unwrap<ClWrap>(args.This());
164167
if (!obj) {
165168
isolate->ThrowError("Invalid ClWrap object");
166169
return;
167170
}
168-
171+
169172
custom_labels_string_t debug_str;
170173
int result = custom_labels_debug_string(obj->underlying_, &debug_str);
171-
174+
172175
if (result != 0) {
173176
isolate->ThrowError("Failed to generate debug string");
174177
return;
175178
}
176-
177-
Local<String> js_string = String::NewFromUtf8(
178-
isolate,
179-
(const char*)debug_str.buf,
180-
NewStringType::kNormal,
181-
debug_str.len
182-
).ToLocalChecked();
183-
184-
free((void*)debug_str.buf);
185-
179+
180+
Local<String> js_string =
181+
String::NewFromUtf8(isolate, (const char *)debug_str.buf,
182+
NewStringType::kNormal, debug_str.len)
183+
.ToLocalChecked();
184+
185+
free((void *)debug_str.buf);
186+
186187
args.GetReturnValue().Set(js_string);
187188
}
188189

189190
void ClWrap::Init(Local<Object> exports) {
190-
Isolate* isolate = exports->GetIsolate();
191+
Isolate *isolate = exports->GetIsolate();
191192
Local<Context> context = isolate->GetCurrentContext();
192193

193194
Local<ObjectTemplate> addon_data_tpl = ObjectTemplate::New(isolate);
194-
addon_data_tpl->SetInternalFieldCount(1); // 1 field for the ClWrap::New()
195+
addon_data_tpl->SetInternalFieldCount(1); // 1 field for the ClWrap::New()
195196
Local<Object> addon_data =
196197
addon_data_tpl->NewInstance(context).ToLocalChecked();
197198

198199
// Prepare constructor template
199200
Local<FunctionTemplate> tpl = FunctionTemplate::New(isolate, New, addon_data);
200201
tpl->SetClassName(String::NewFromUtf8(isolate, "ClWrap").ToLocalChecked());
201202
tpl->InstanceTemplate()->SetInternalFieldCount(1);
202-
203+
203204
// Add toString method
204205
tpl->PrototypeTemplate()->Set(
205-
String::NewFromUtf8(isolate, "toString").ToLocalChecked(),
206-
FunctionTemplate::New(isolate, ToString)
207-
);
206+
String::NewFromUtf8(isolate, "toString").ToLocalChecked(),
207+
FunctionTemplate::New(isolate, ToString));
208208

209209
Local<Function> constructor = tpl->GetFunction(context).ToLocalChecked();
210210
addon_data->SetInternalField(0, constructor);
211-
exports->Set(context, String::NewFromUtf8(
212-
isolate, "ClWrap").ToLocalChecked(),
213-
constructor).FromJust();
211+
exports
212+
->Set(context, String::NewFromUtf8(isolate, "ClWrap").ToLocalChecked(),
213+
constructor)
214+
.FromJust();
214215
}
215216

216-
void StoreHash(const v8::FunctionCallbackInfo<v8::Value>& args) {
217+
void StoreHash(const v8::FunctionCallbackInfo<v8::Value> &args) {
217218
Isolate *isolate = args.GetIsolate();
218219
if (!args[0]->IsObject()) {
219220
isolate->ThrowError("First argument must be an object.");
@@ -223,7 +224,7 @@ void StoreHash(const v8::FunctionCallbackInfo<v8::Value>& args) {
223224
custom_labels_als_identity_hash = hash;
224225
custom_labels_als_handle = Global<Object>(isolate, obj);
225226
}
226-
227+
227228
#pragma GCC diagnostic push
228229
#pragma GCC diagnostic ignored "-Wcast-function-type"
229230

package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
2-
"name": "@polarsignals/custom-labels-experiment-1",
3-
"version": "0.0.3",
2+
"name": "@polarsignals/custom-labels",
3+
"version": "0.3.1",
44
"description": "test",
55
"main": "js/index.js",
66
"scripts": {

0 commit comments

Comments
 (0)