GeezerVisonThe World Wide Web, restored to 800 pixels of glory. |
| Home|About|Gaming cabinet|Archives|How it works|Starter sites |
GeezerVison is showing github.com — ios/NativeScript/runtime/Pointer.cpp at main · NativeScript/ios Expand file tree/ Copy pathPointer.cppMore file actions 380 lines (323 loc) · 14 KB / Copy pathPointer.cppFile metadata and controls380 lines (323 loc) · 14 KB 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 #include "Pointer.h" #include "Caches.h" #include "Constants.h" #include "Helpers.h" #include "NativeScriptException.h" #include "ObjectManager.h" using namespace v8; namespace tns { void Pointer::Register(Local<Context> context, Local<Object> interop) { Isolate* isolate = v8::Isolate::GetCurrent(); Local<v8::Function> ctorFunc = Pointer::GetPointerCtorFunc(context); bool success = interop->Set(context, tns::ToV8String(isolate, "Pointer"), ctorFunc) .FromMaybe(false); tns::Assert(success, isolate); } Local<Value> Pointer::NewInstance(Local<Context> context, void* handle) { Isolate* isolate = v8::Isolate::GetCurrent(); intptr_t ptr = static_cast<intptr_t>(reinterpret_cast<size_t>(handle)); Local<Value> arg = BigInt::NewFromUnsigned(isolate, ptr); Local<Value> args[1]{arg}; Local<Value> result; Local<v8::Function> ctorFunc = Pointer::GetPointerCtorFunc(context); bool success = ctorFunc->NewInstance(isolate->GetCurrentContext(), 1, args) .ToLocal(&result); if (!success) { return v8::Undefined(isolate); } return result; } Local<v8::Function> Pointer::GetPointerCtorFunc(Local<Context> context) { Isolate* isolate = v8::Isolate::GetCurrent(); auto cache = Caches::Get(isolate); Persistent<v8::Function>* pointerCtorFunc = cache->PointerCtorFunc.get(); if (pointerCtorFunc != nullptr) { return pointerCtorFunc->Get(isolate); } Local<FunctionTemplate> ctorFuncTemplate = FunctionTemplate::New(isolate, PointerConstructorCallback); ctorFuncTemplate->InstanceTemplate()->SetInternalFieldCount(1); ctorFuncTemplate->SetClassName(tns::ToV8String(isolate, "Pointer")); Local<v8::Function> ctorFunc; if (!ctorFuncTemplate->GetFunction(context).ToLocal(&ctorFunc)) { tns::Assert(false, isolate); } tns::SetValue(isolate, ctorFunc, new PointerTypeWrapper()); Local<Value> prototypeValue; bool success = ctorFunc->Get(context, tns::ToV8String(isolate, "prototype")) .ToLocal(&prototypeValue); tns::Assert(success && prototypeValue->IsObject(), isolate); Local<Object> prototype = prototypeValue.As<Object>(); Pointer::RegisterAddMethod(context, prototype); Pointer::RegisterSubtractMethod(context, prototype); Pointer::RegisterToStringMethod(context, prototype); Pointer::RegisterToHexStringMethod(context, prototype); Pointer::RegisterToDecimalStringMethod(context, prototype); Pointer::RegisterToNumberMethod(context, prototype); Pointer::RegisterToBigIntMethod(context, prototype); cache->PointerCtorFunc = std::make_unique<Persistent<v8::Function>>(isolate, ctorFunc); cache->PointerCtorFunc->SetWrapperClassId(Constants::ClassTypes::DataWrapper); return ctorFunc; } void Pointer::PointerConstructorCallback( const FunctionCallbackInfo<Value>& info) { Isolate* isolate = info.GetIsolate(); Local<Context> context = isolate->GetCurrentContext(); try { void* ptr = nullptr; if (info.Length() == 1) { bool isBigInt = tns::IsBigInt(info[0]); bool isNumber = !isBigInt && tns::IsNumber(info[0]); if (!isBigInt && !isNumber) { throw NativeScriptException( "Pointer constructor's first arg must be an integer."); } #if __SIZEOF_POINTER__ == 8 // JSC stores 64-bit integers as doubles in JSValue. // Caution: This means that pointers with more than 54 significant bits // are likely to be rounded and misrepresented! // However, current OS and hardware implementations are using 48 bits, // so we're safe at the time being. // See https://en.wikipedia.org/wiki/X86-64#Virtual_address_space_details // and https://en.wikipedia.org/wiki/ARM_architecture#ARMv8-A // The future is here, and turns out the OS is using more than 54 bits. // as a result, int64_t value; if (isBigInt) { value = info[0].As<BigInt>()->Int64Value(); } else { // TODO: Maybe log this? // #ifdef DEBUG // syslog(LOG_WARNING, "Using JS Number to represent a // pointer. Result might be wrong."); // #endif tns::Assert(info[0].As<Number>()->IntegerValue(context).To(&value), isolate); } ptr = reinterpret_cast<void*>(value); #else int32_t value; if (isBigInt) { value = (int32_t)info[0].As<BigInt>()->Int64Value(); } else { tns::Assert(info[0].As<Number>()->Int32Value(context).To(&value), isolate); } ptr = reinterpret_cast<void*>(value); #endif } auto cache = Caches::Get(isolate); auto it = cache->PointerInstances.find(ptr); if (it != cache->PointerInstances.end()) { info.GetReturnValue().Set(it->second->Get(isolate)); return; } PointerWrapper* wrapper = new PointerWrapper(ptr); tns::SetValue(isolate, info.This(), wrapper); ObjectManager::Register(context, info.This()); cache->PointerInstances.emplace( ptr, std::make_shared<Persistent<Object>>(isolate, info.This())); } catch (NativeScriptException& ex) { ex.ReThrowToV8(isolate); } } void Pointer::RegisterAddMethod(Local<Context> context, Local<Object> prototype) { Isolate* isolate = v8::Isolate::GetCurrent(); Local<FunctionTemplate> funcTemplate = FunctionTemplate::New( isolate, [](const FunctionCallbackInfo<Value>& info) { Isolate* isolate = info.GetIsolate(); Local<Context> context = isolate->GetCurrentContext(); tns::Assert(info.Length() == 1 && tns::IsNumber(info[0]), isolate); BaseDataWrapper* baseWrapper = tns::GetValueOrReport(isolate, info.This(), "Pointer.add"); if (baseWrapper == nullptr) { return; } PointerWrapper* wrapper = static_cast<PointerWrapper*>(baseWrapper); void* value = wrapper->Data(); int32_t offset; tns::Assert(info[0].As<Number>()->Int32Value(context).To(&offset), isolate); void* newValue = reinterpret_cast<void*>(reinterpret_cast<char*>(value) + offset); Local<Value> result = Pointer::NewInstance(context, newValue); info.GetReturnValue().Set(result); }); Local<v8::Function> func; tns::Assert(funcTemplate->GetFunction(context).ToLocal(&func), isolate); bool success = prototype->Set(context, tns::ToV8String(isolate, "add"), func) .FromMaybe(false); tns::Assert(success, isolate); } void Pointer::RegisterSubtractMethod(Local<Context> context, Local<Object> prototype) { Isolate* isolate = v8::Isolate::GetCurrent(); Local<FunctionTemplate> funcTemplate = FunctionTemplate::New( isolate, [](const FunctionCallbackInfo<Value>& info) { Isolate* isolate = info.GetIsolate(); Local<Context> context = isolate->GetCurrentContext(); tns::Assert(info.Length() == 1 && tns::IsNumber(info[0]), isolate); BaseDataWrapper* baseWrapper = tns::GetValueOrReport(isolate, info.This(), "Pointer.subtract"); if (baseWrapper == nullptr) { return; } PointerWrapper* wrapper = static_cast<PointerWrapper*>(baseWrapper); void* value = wrapper->Data(); int32_t offset; tns::Assert(info[0].As<Number>()->Int32Value(context).To(&offset), isolate); void* newValue = reinterpret_cast<void*>(reinterpret_cast<char*>(value) - offset); intptr_t newValuePtr = static_cast<intptr_t>(reinterpret_cast<size_t>(newValue)); Local<v8::Function> ctorFunc = Pointer::GetPointerCtorFunc(context); Local<Value> arg = Number::New(isolate, newValuePtr); Local<Value> args[1]{arg}; Local<Value> result; bool success = ctorFunc->NewInstance(context, 1, args).ToLocal(&result); tns::Assert(success, isolate); info.GetReturnValue().Set(result); }); Local<v8::Function> func; tns::Assert(funcTemplate->GetFunction(context).ToLocal(&func), isolate); bool success = prototype->Set(context, tns::ToV8String(isolate, "subtract"), func) .FromMaybe(false); tns::Assert(success, isolate); } void Pointer::RegisterToStringMethod(Local<Context> context, Local<Object> prototype) { Isolate* isolate = v8::Isolate::GetCurrent(); Local<FunctionTemplate> funcTemplate = FunctionTemplate::New( isolate, [](const FunctionCallbackInfo<Value>& info) { Isolate* isolate = info.GetIsolate(); BaseDataWrapper* baseWrapper = tns::GetValueOrReport(isolate, info.This(), "Pointer.toString"); if (baseWrapper == nullptr) { // Logging paths stringify objects incidentally; string coercion of // a released Pointer must stay total under the report policy. if (tns::GetReleasedObjectPolicy() == tns::ReleasedObjectPolicy::kReport) { info.GetReturnValue().Set( tns::ToV8String(isolate, "<Pointer: released>")); } return; } PointerWrapper* wrapper = static_cast<PointerWrapper*>(baseWrapper); void* value = wrapper->Data(); char buffer[100]; snprintf(buffer, 100, "<Pointer: %p>", value); Local<v8::String> result = tns::ToV8String(isolate, buffer); info.GetReturnValue().Set(result); }); Local<v8::Function> func; tns::Assert(funcTemplate->GetFunction(context).ToLocal(&func), isolate); bool success = prototype->Set(context, tns::ToV8String(isolate, "toString"), func) .FromMaybe(false); tns::Assert(success, isolate); } void Pointer::RegisterToHexStringMethod(Local<Context> context, Local<Object> prototype) { Isolate* isolate = v8::Isolate::GetCurrent(); Local<FunctionTemplate> funcTemplate = FunctionTemplate::New( isolate, [](const FunctionCallbackInfo<Value>& info) { Isolate* isolate = info.GetIsolate(); BaseDataWrapper* baseWrapper = tns::GetValueOrReport(isolate, info.This(), "Pointer.toHexString"); if (baseWrapper == nullptr) { return; } PointerWrapper* wrapper = static_cast<PointerWrapper*>(baseWrapper); const void* value = wrapper->Data(); char buffer[100]; snprintf(buffer, 100, "%p", value); Local<v8::String> result = tns::ToV8String(isolate, buffer); info.GetReturnValue().Set(result); }); Local<v8::Function> func; tns::Assert(funcTemplate->GetFunction(context).ToLocal(&func), isolate); bool success = prototype->Set(context, tns::ToV8String(isolate, "toHexString"), func) .FromMaybe(false); tns::Assert(success, isolate); } void Pointer::RegisterToDecimalStringMethod(Local<Context> context, Local<Object> prototype) { Isolate* isolate = v8::Isolate::GetCurrent(); Local<FunctionTemplate> funcTemplate = FunctionTemplate::New( isolate, [](const FunctionCallbackInfo<Value>& info) { Isolate* isolate = info.GetIsolate(); BaseDataWrapper* baseWrapper = tns::GetValueOrReport( isolate, info.This(), "Pointer.toDecimalString"); if (baseWrapper == nullptr) { return; } PointerWrapper* wrapper = static_cast<PointerWrapper*>(baseWrapper); const void* value = wrapper->Data(); intptr_t ptr = static_cast<intptr_t>(reinterpret_cast<size_t>(value)); char buffer[100]; snprintf(buffer, 100, "%ld", ptr); Local<v8::String> result = tns::ToV8String(isolate, buffer); info.GetReturnValue().Set(result); }); Local<v8::Function> func; tns::Assert(funcTemplate->GetFunction(context).ToLocal(&func), isolate); bool success = prototype->Set(context, tns::ToV8String(isolate, "toDecimalString"), func) .FromMaybe(false); tns::Assert(success, isolate); } void Pointer::RegisterToNumberMethod(Local<Context> context, Local<Object> prototype) { Isolate* isolate = v8::Isolate::GetCurrent(); Local<FunctionTemplate> funcTemplate = FunctionTemplate::New( isolate, [](const FunctionCallbackInfo<Value>& info) { Isolate* isolate = info.GetIsolate(); BaseDataWrapper* baseWrapper = tns::GetValueOrReport(isolate, info.This(), "Pointer.toNumber"); if (baseWrapper == nullptr) { return; } PointerWrapper* wrapper = static_cast<PointerWrapper*>(baseWrapper); const void* value = wrapper->Data(); size_t number = reinterpret_cast<size_t>(value); Local<Number> result = Number::New(isolate, number); info.GetReturnValue().Set(result); }); Local<v8::Function> func; tns::Assert(funcTemplate->GetFunction(context).ToLocal(&func), isolate); bool success = prototype->Set(context, tns::ToV8String(isolate, "toNumber"), func) .FromMaybe(false); tns::Assert(success, isolate); } void Pointer::RegisterToBigIntMethod(Local<Context> context, Local<Object> prototype) { Isolate* isolate = v8::Isolate::GetCurrent(); Local<FunctionTemplate> funcTemplate = FunctionTemplate::New( isolate, [](const FunctionCallbackInfo<Value>& info) { Isolate* isolate = info.GetIsolate(); BaseDataWrapper* baseWrapper = tns::GetValueOrReport(isolate, info.This(), "Pointer.toBigInt"); if (baseWrapper == nullptr) { return; } PointerWrapper* wrapper = static_cast<PointerWrapper*>(baseWrapper); const void* value = wrapper->Data(); size_t number = reinterpret_cast<size_t>(value); Local<BigInt> result = BigInt::NewFromUnsigned(isolate, number); info.GetReturnValue().Set(result); }); Local<v8::Function> func; tns::Assert(funcTemplate->GetFunction(context).ToLocal(&func), isolate); bool success = prototype->Set(context, tns::ToV8String(isolate, "toBigInt"), func) .FromMaybe(false); tns::Assert(success, isolate); } } // namespace tns |
| You are visitor 01402520 — GeezerVison does not store the pages you look up. Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required. |