@@ -15,9 +15,38 @@ extern "C" void NSLog(CFStringRef format, ...);
15
15
16
16
namespace tns {
17
17
18
- inline v8::Local<v8::String> ToV8String (v8::Isolate* isolate, std::string value) {
18
+ inline v8::Local<v8::String> ToV8String (v8::Isolate* isolate,const std::string& value) {
19
19
return v8::String::NewFromUtf8 (isolate, value.c_str (), v8::NewStringType::kNormal , (int )value.length ()).ToLocalChecked ();
20
20
}
21
+ #ifdef __OBJC__
22
+ inline v8::Local<v8::String> ToV8String (v8::Isolate* isolate,const NSString* value) {
23
+ /*
24
+ // TODO: profile if this is faster
25
+ // maybe have multiple conversion
26
+ if([value fastestEncoding] == NSUTF16StringEncoding) {
27
+ uint16_t static_buffer[256];
28
+ uint16_t* targetBuffer = static_buffer;
29
+ bool isDynamic = false;
30
+ auto length = [value maximumLengthOfBytesUsingEncoding:NSUTF16StringEncoding];
31
+ auto numberOfBytes = length * sizeof(uint16_t);
32
+ if (length > 256) {
33
+ targetBuffer = (uint16_t*)malloc(numberOfBytes);
34
+ isDynamic = true;
35
+ }
36
+ NSUInteger usedLength = 0;
37
+ NSRange range = NSMakeRange(0, [value length]);
38
+ [value getBytes:targetBuffer maxLength:numberOfBytes usedLength:&usedLength encoding:NSUTF16StringEncoding options:0 range:range remainingRange:NULL];
39
+
40
+ auto result = v8::String::NewFromTwoByte(isolate, targetBuffer, v8::NewStringType::kNormal, (int)[value length]).ToLocalChecked();
41
+ if (isDynamic) {
42
+ free(targetBuffer);
43
+ }
44
+ return result;
45
+ }
46
+ */
47
+ return v8::String::NewFromUtf8 (isolate, [value UTF8String], v8::NewStringType::kNormal , (int )[value lengthOfBytesUsingEncoding:NSUTF8StringEncoding]).ToLocalChecked ();
48
+ }
49
+ #endif
21
50
inline std::string ToString (v8::Isolate* isolate, const v8::Local<v8::Value>& value) {
22
51
if (value.IsEmpty ()) {
23
52
return std::string ();
@@ -35,8 +64,34 @@ inline std::string ToString(v8::Isolate* isolate, const v8::Local<v8::Value>& va
35
64
return std::string ();
36
65
}
37
66
38
- return std::string (*result);
67
+ return std::string (*result, result. length () );
39
68
}
69
+
70
+ #ifdef __OBJC__
71
+ inline NSString* ToNSString (const std::string& v) {
72
+ return [[NSString alloc] initWithBytes:v.c_str () length:v.length () encoding:NSUTF8StringEncoding];
73
+ }
74
+ // this method is a copy of ToString to avoid needless std::string<->NSString conversions
75
+ inline NSString* ToNSString (v8::Isolate* isolate, const v8::Local<v8::Value>& value) {
76
+ if (value.IsEmpty ()) {
77
+ return @" " ;
78
+ }
79
+
80
+ if (value->IsStringObject ()) {
81
+ v8::Local<v8::String> obj = value.As <v8::StringObject>()->ValueOf ();
82
+ return ToNSString (isolate, obj);
83
+ }
84
+
85
+ v8::String::Utf8Value result (isolate, value);
86
+
87
+ const char * val = *result;
88
+ if (val == nullptr ) {
89
+ return @" " ;
90
+ }
91
+
92
+ return [[NSString alloc] initWithBytes:*result length:result.length () encoding:NSUTF8StringEncoding];
93
+ }
94
+ #endif
40
95
std::u16string ToUtf16String (v8::Isolate* isolate, const v8::Local<v8::Value>& value);
41
96
inline double ToNumber (v8::Isolate* isolate, const v8::Local<v8::Value>& value) {
42
97
double result = NAN;
0 commit comments