11cb0ef41Sopenharmony_ci#include <node.h>
21cb0ef41Sopenharmony_ci#include <node_buffer.h>
31cb0ef41Sopenharmony_ci#include <v8.h>
41cb0ef41Sopenharmony_ci
51cb0ef41Sopenharmony_ci#include <assert.h>
61cb0ef41Sopenharmony_ci
71cb0ef41Sopenharmony_cistatic int alive;
81cb0ef41Sopenharmony_ci
91cb0ef41Sopenharmony_cistatic void FreeCallback(char* data, void* hint) {
101cb0ef41Sopenharmony_ci  assert(data == nullptr);
111cb0ef41Sopenharmony_ci  alive--;
121cb0ef41Sopenharmony_ci}
131cb0ef41Sopenharmony_ci
141cb0ef41Sopenharmony_civoid IsAlive(const v8::FunctionCallbackInfo<v8::Value>& args) {
151cb0ef41Sopenharmony_ci  args.GetReturnValue().Set(alive);
161cb0ef41Sopenharmony_ci}
171cb0ef41Sopenharmony_ci
181cb0ef41Sopenharmony_civoid Run(const v8::FunctionCallbackInfo<v8::Value>& args) {
191cb0ef41Sopenharmony_ci  v8::Isolate* isolate = args.GetIsolate();
201cb0ef41Sopenharmony_ci  alive++;
211cb0ef41Sopenharmony_ci
221cb0ef41Sopenharmony_ci  {
231cb0ef41Sopenharmony_ci    v8::HandleScope scope(isolate);
241cb0ef41Sopenharmony_ci    v8::Local<v8::Object> buf = node::Buffer::New(
251cb0ef41Sopenharmony_ci          isolate,
261cb0ef41Sopenharmony_ci          nullptr,
271cb0ef41Sopenharmony_ci          0,
281cb0ef41Sopenharmony_ci          FreeCallback,
291cb0ef41Sopenharmony_ci          nullptr).ToLocalChecked();
301cb0ef41Sopenharmony_ci
311cb0ef41Sopenharmony_ci    char* data = node::Buffer::Data(buf);
321cb0ef41Sopenharmony_ci    assert(data == nullptr);
331cb0ef41Sopenharmony_ci  }
341cb0ef41Sopenharmony_ci}
351cb0ef41Sopenharmony_ci
361cb0ef41Sopenharmony_civoid init(v8::Local<v8::Object> exports) {
371cb0ef41Sopenharmony_ci  NODE_SET_METHOD(exports, "run", Run);
381cb0ef41Sopenharmony_ci  NODE_SET_METHOD(exports, "isAlive", IsAlive);
391cb0ef41Sopenharmony_ci}
401cb0ef41Sopenharmony_ci
411cb0ef41Sopenharmony_ciNODE_MODULE(NODE_GYP_MODULE_NAME, init)
42