|
15 | 15 | #include "Reference.h" |
16 | 16 | #include "Pointer.h" |
17 | 17 | #include "ExtVector.h" |
| 18 | +#include "RuntimeConfig.h" |
18 | 19 | #include "SymbolIterator.h" |
19 | 20 | #include "UnmanagedType.h" |
20 | 21 | #include "OneByteStringResource.h" |
|
144 | 145 |
|
145 | 146 | void Interop::WriteValue(Local<Context> context, const TypeEncoding* typeEncoding, void* dest, Local<Value> arg) { |
146 | 147 | Isolate* isolate = context->GetIsolate(); |
147 | | - |
| 148 | + ExecuteWriteValueDebugValidationsIfInDebug(context, typeEncoding, dest, arg); |
148 | 149 | if (arg.IsEmpty() || arg->IsNullOrUndefined()) { |
149 | 150 | ffi_type* ffiType = FFICall::GetArgumentType(typeEncoding, true); |
150 | 151 | size_t size = ffiType->size; |
|
1483 | 1484 | return result; |
1484 | 1485 | } |
1485 | 1486 |
|
| 1487 | +// MARK: - Debug Messages for the runtime |
| 1488 | + |
| 1489 | +void ExecuteWriteValueValidationsAndStopExecutionAndLogStackTrace(Local<Context> context, const TypeEncoding* typeEncoding, void* dest, Local<Value> arg) { |
| 1490 | + if (!RuntimeConfig.IsDebug) { |
| 1491 | + return; |
| 1492 | + } |
| 1493 | + Isolate* isolate = context->GetIsolate(); |
| 1494 | + std::string destName = typeEncoding->details.interfaceDeclarationReference.name.valuePtr(); |
| 1495 | + Local<Value> originArg = arg; |
| 1496 | + if (typeEncoding->type == BinaryTypeEncodingType::InterfaceDeclarationReference) { |
| 1497 | + if (originArg->IsObject()) { |
| 1498 | + Local<Object> originObj = originArg.As<Object>(); |
| 1499 | + if ((originObj->IsArrayBuffer() || originObj->IsArrayBufferView()) && |
| 1500 | + destName != "NSArray") { |
| 1501 | + tns::StopExecutionAndLogStackTrace(isolate); |
| 1502 | + } |
| 1503 | + } |
| 1504 | + if (destName == "NSString" && tns::IsNumber(originArg)) { |
| 1505 | + tns::StopExecutionAndLogStackTrace(isolate); |
| 1506 | + } |
| 1507 | + if (destName == "NSString" && tns::IsBool(originArg)) { |
| 1508 | + tns::StopExecutionAndLogStackTrace(isolate); |
| 1509 | + } |
| 1510 | + if (destName == "NSString" && tns::IsArrayOrArrayLike(isolate, originArg)) { |
| 1511 | + tns::StopExecutionAndLogStackTrace(isolate); |
| 1512 | + } |
| 1513 | + } |
| 1514 | +} |
| 1515 | + |
| 1516 | +bool IsTypeEncondingHandldedByDebugMessages(const TypeEncoding* typeEncoding) { |
| 1517 | + if (typeEncoding->type != BinaryTypeEncodingType::InterfaceDeclarationReference && |
| 1518 | + typeEncoding->type != BinaryTypeEncodingType::StructDeclarationReference && |
| 1519 | + typeEncoding->type != BinaryTypeEncodingType::IdEncoding) { |
| 1520 | + return true; |
| 1521 | + } else { |
| 1522 | + return false; |
| 1523 | + } |
| 1524 | +} |
| 1525 | + |
| 1526 | +void LogWriteValueTraceMessage(Local<Context> context, const TypeEncoding* typeEncoding, void* dest, Local<Value> arg) { |
| 1527 | + if (!RuntimeConfig.IsDebug) { |
| 1528 | + return; |
| 1529 | + } |
| 1530 | + Isolate* isolate = context->GetIsolate(); |
| 1531 | + std::string destName = typeEncoding->details.interfaceDeclarationReference.name.valuePtr(); |
| 1532 | + std::string originName = tns::ToString(isolate, arg); |
| 1533 | + if (originName == "") { |
| 1534 | + // empty string |
| 1535 | + originName = "\"\""; |
| 1536 | + } |
| 1537 | + NSString* message = [NSString stringWithFormat:@"Interop::WriteValue: from {%s} to {%s}", originName.c_str(), destName.c_str()]; |
| 1538 | + Log(@"%@", message); |
| 1539 | +} |
| 1540 | + |
| 1541 | +void Interop::ExecuteWriteValueDebugValidationsIfInDebug(Local<Context> context, const TypeEncoding* typeEncoding, void* dest, Local<Value> arg) { |
| 1542 | + if (!RuntimeConfig.IsDebug) { |
| 1543 | + return; |
| 1544 | + } |
| 1545 | + if (arg.IsEmpty() || arg->IsNullOrUndefined()) { |
| 1546 | + return; |
| 1547 | + } |
| 1548 | + if (IsTypeEncondingHandldedByDebugMessages(typeEncoding)) { |
| 1549 | + return; |
| 1550 | + } |
| 1551 | + LogWriteValueTraceMessage(context, typeEncoding, dest, arg); |
| 1552 | + ExecuteWriteValueValidationsAndStopExecutionAndLogStackTrace(context, typeEncoding, dest, arg); |
| 1553 | +} |
| 1554 | + |
1486 | 1555 | } |
0 commit comments