koffi 2.10.1 → 2.11.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -2107,6 +2107,44 @@ static Napi::Value EncodeValue(const Napi::CallbackInfo &info)
2107
2107
  return env.Undefined();
2108
2108
  }
2109
2109
 
2110
+ static Napi::Value CreateView(const Napi::CallbackInfo &info)
2111
+ {
2112
+ Napi::Env env = info.Env();
2113
+ InstanceData *instance = env.GetInstanceData<InstanceData>();
2114
+
2115
+ if (info.Length() < 1) {
2116
+ ThrowError<Napi::TypeError>(env, "Expected 2 arguments, got %1", info.Length());
2117
+ return env.Null();
2118
+ }
2119
+ if (!info[1].IsNumber()) {
2120
+ ThrowError<Napi::TypeError>(env, "Unexpected %1 value for length, expected integer", GetValueType(instance, info[1]));
2121
+ return env.Null();
2122
+ }
2123
+
2124
+ void *ptr = nullptr;
2125
+ if (!GetExternalPointer(env, info[0], &ptr))
2126
+ return env.Null();
2127
+ Size len = (Size)info[1].As<Napi::Number>().Int64Value();
2128
+
2129
+ if (len < 0) {
2130
+ ThrowError<Napi::TypeError>(env, "Array length must be positive and non-zero");
2131
+ return env.Null();
2132
+ }
2133
+
2134
+ if (len) {
2135
+ Napi::ArrayBuffer view = Napi::ArrayBuffer::New(env, ptr, (size_t)len);
2136
+
2137
+ if (!view.ByteLength()) {
2138
+ ThrowError<Napi::Error>(env, "This runtime does not support external buffers");
2139
+ return env.Null();
2140
+ }
2141
+
2142
+ return view;
2143
+ } else {
2144
+ return Napi::ArrayBuffer::New(env, 0);
2145
+ }
2146
+ }
2147
+
2110
2148
  static Napi::Value ResetKoffi(const Napi::CallbackInfo &info)
2111
2149
  {
2112
2150
  Napi::Env env = info.Env();
@@ -2364,6 +2402,7 @@ static Napi::Object InitModule(Napi::Env env, Napi::Object exports)
2364
2402
  exports.Set("address", Napi::Function::New(env, GetPointerAddress, "address"));
2365
2403
  exports.Set("call", Napi::Function::New(env, CallPointerSync, "call"));
2366
2404
  exports.Set("encode", Napi::Function::New(env, EncodeValue, "encode"));
2405
+ exports.Set("view", Napi::Function::New(env, CreateView, "view"));
2367
2406
 
2368
2407
  exports.Set("reset", Napi::Function::New(env, ResetKoffi, "reset"));
2369
2408