|
25 | 25 | Pointer::Register(context, interop);
|
26 | 26 | FunctionReference::Register(context, interop);
|
27 | 27 | RegisterBufferFromDataFunction(context, interop);
|
| 28 | + RegisterStringFromCString(context, interop); |
28 | 29 | RegisterHandleOfFunction(context, interop);
|
29 | 30 | RegisterAllocFunction(context, interop);
|
30 | 31 | RegisterFreeFunction(context, interop);
|
|
140 | 141 | tns::Assert(success, isolate);
|
141 | 142 | }
|
142 | 143 |
|
| 144 | +void Interop::RegisterStringFromCString(Local<Context> context, Local<Object> interop) { |
| 145 | + Local<v8::Function> func; |
| 146 | + bool success = v8::Function::New(context, [](const FunctionCallbackInfo<Value>& info) { |
| 147 | + Isolate* isolate = info.GetIsolate(); |
| 148 | + tns::Assert(info.Length() >= 1 && info[0]->IsObject(), isolate); |
| 149 | + Local<Object> arg = info[0].As<Object>(); |
| 150 | + int stringLength = -1; |
| 151 | + if(info.Length() >= 2 && !info[1].IsEmpty() && !info[1]->IsNullOrUndefined()) { |
| 152 | + auto desiredLength = ToNumber(isolate, info[1]); |
| 153 | + if (desiredLength != NAN) { |
| 154 | + stringLength = desiredLength; |
| 155 | + } |
| 156 | + } |
| 157 | + tns::Assert(arg->InternalFieldCount() > 0 && arg->GetInternalField(0)->IsExternal(), isolate); |
| 158 | + |
| 159 | + Local<External> ext = arg->GetInternalField(0).As<External>(); |
| 160 | + BaseDataWrapper* wrapper = static_cast<BaseDataWrapper*>(ext->Value()); |
| 161 | + tns::Assert(wrapper != nullptr); |
| 162 | + char* data = nullptr; |
| 163 | + switch (wrapper->Type()) { |
| 164 | + case WrapperType::Pointer: |
| 165 | + { |
| 166 | + PointerWrapper* pointerWrapper = static_cast<PointerWrapper*>(wrapper); |
| 167 | + data = static_cast<char*>(pointerWrapper->Data()); |
| 168 | + } |
| 169 | + break; |
| 170 | + case WrapperType::Reference: |
| 171 | + { |
| 172 | + ReferenceWrapper* referenceWrapper = static_cast<ReferenceWrapper*>(wrapper); |
| 173 | + if (referenceWrapper->Data() != nullptr) { |
| 174 | + data = static_cast<char*>(referenceWrapper->Data()); |
| 175 | + break; |
| 176 | + } |
| 177 | + auto wrappedValue = referenceWrapper->Value()->Get(isolate); |
| 178 | + auto wrappedWrapper = tns::GetValue(isolate, wrappedValue); |
| 179 | + tns::Assert(wrappedWrapper->Type() == WrapperType::Pointer); |
| 180 | + data = static_cast<char*>((static_cast<PointerWrapper*>(wrappedWrapper))->Data()); |
| 181 | + } |
| 182 | + default: |
| 183 | + break; |
| 184 | + } |
| 185 | + tns::Assert(data != nullptr); |
| 186 | + |
| 187 | + auto result = v8::String::NewFromUtf8(isolate, data, v8::NewStringType::kNormal, stringLength).ToLocalChecked(); |
| 188 | + info.GetReturnValue().Set(result); |
| 189 | + }).ToLocal(&func); |
| 190 | + |
| 191 | + Isolate* isolate = context->GetIsolate(); |
| 192 | + tns::Assert(success, isolate); |
| 193 | + |
| 194 | + success = interop->Set(context, tns::ToV8String(isolate, "stringFromCString"), func).FromMaybe(false); |
| 195 | + tns::Assert(success, isolate); |
| 196 | +} |
| 197 | + |
143 | 198 | void Interop::RegisterHandleOfFunction(Local<Context> context, Local<Object> interop) {
|
144 | 199 | Local<v8::Function> func;
|
145 | 200 | bool success = v8::Function::New(context, [](const FunctionCallbackInfo<Value>& info) {
|
|
0 commit comments