Create NSDataBigString

Reviewed By: bnham

Differential Revision: D4559734

fbshipit-source-id: 6766093524ae79ed2a41285d33eb2207993a7d0e
This commit is contained in:
Pieter De Baets 2017-02-20 04:57:54 -08:00 committed by Facebook Github Bot
parent 1635c02e92
commit ebe3355de7
5 changed files with 657 additions and 196 deletions

View File

@ -0,0 +1,40 @@
/**
* Copyright (c) 2015-present, Facebook, Inc.
* All rights reserved.
*
* This source code is licensed under the BSD-style license found in the
* LICENSE file in the root directory of this source tree. An additional grant
* of patent rights can be found in the PATENTS file in the same directory.
*/
#import <Foundation/Foundation.h>
#include <cxxreact/JSBigString.h>
namespace facebook {
namespace react {
class NSDataBigString : public JSBigString {
public:
// The NSData passed in must be be null-terminated.
NSDataBigString(NSData *data);
// The ASCII optimization is not enabled on iOS
bool isAscii() const override {
return false;
}
const char *c_str() const override {
return (const char *)[m_data bytes];
}
size_t size() const override {
return m_length;
}
private:
NSData *m_data;
size_t m_length;
};
} }

View File

@ -0,0 +1,44 @@
/**
* Copyright (c) 2015-present, Facebook, Inc.
* All rights reserved.
*
* This source code is licensed under the BSD-style license found in the
* LICENSE file in the root directory of this source tree. An additional grant
* of patent rights can be found in the PATENTS file in the same directory.
*/
#import "NSDataBigString.h"
namespace facebook {
namespace react {
static NSData *ensureNullTerminated(NSData *source)
{
if (!source || source.length == 0) {
return nil;
}
NSUInteger sourceLength = source.length;
unsigned char lastByte;
[source getBytes:&lastByte range:NSMakeRange(sourceLength - 1, 1)];
// TODO: bundles from the packager should always include a NULL byte
// or we should we relax this requirement and only read as much from the
// buffer as length indicates
if (lastByte == '\0') {
return source;
} else {
NSMutableData *data = [source mutableCopy];
unsigned char nullByte = '\0';
[data appendBytes:&nullByte length:1];
return data;
}
}
NSDataBigString::NSDataBigString(NSData *data)
{
m_length = [data length];
m_data = ensureNullTerminated(data);
}
} }

View File

@ -40,6 +40,7 @@
#import <cxxreact/Platform.h> #import <cxxreact/Platform.h>
#import <jschelpers/Value.h> #import <jschelpers/Value.h>
#import "NSDataBigString.h"
#import "RCTMessageThread.h" #import "RCTMessageThread.h"
#import "RCTNativeModule.h" #import "RCTNativeModule.h"
#import "RCTObjcExecutor.h" #import "RCTObjcExecutor.h"
@ -1183,10 +1184,7 @@ RCT_NOT_IMPLEMENTED(- (instancetype)initWithBundleURL:(__unused NSURL *)bundleUR
self->_reactInstance->loadUnbundle(std::move(ramBundle), std::move(scriptStr), self->_reactInstance->loadUnbundle(std::move(ramBundle), std::move(scriptStr),
[[url absoluteString] UTF8String]); [[url absoluteString] UTF8String]);
} else if (self->_reactInstance) { } else if (self->_reactInstance) {
auto bigbuf = std::make_unique<JSBigBufferString>([script length]); self->_reactInstance->loadScriptFromString(std::make_unique<NSDataBigString>(script),
memcpy(bigbuf->data(), [script bytes], bigbuf->size());
self->_reactInstance->loadScriptFromString(std::move(bigbuf),
[[url absoluteString] UTF8String]); [[url absoluteString] UTF8String]);
} }
}]; }];
@ -1210,10 +1208,8 @@ RCT_NOT_IMPLEMENTED(- (instancetype)initWithBundleURL:(__unused NSURL *)bundleUR
[[url absoluteString] UTF8String]); [[url absoluteString] UTF8String]);
} }
} else if (self->_reactInstance) { } else if (self->_reactInstance) {
auto bigbuf = std::make_unique<JSBigBufferString>([script length]); self->_reactInstance->loadScriptFromStringSync(std::make_unique<NSDataBigString>(script),
memcpy(bigbuf->data(), [script bytes], bigbuf->size()); [[url absoluteString] UTF8String]);
self->_reactInstance->loadScriptFromStringSync(std::move(bigbuf), [[url absoluteString] UTF8String]);
} else { } else {
throw std::logic_error( throw std::logic_error(
"Attempt to call loadApplicationScriptSync: on uninitialized bridge"); "Attempt to call loadApplicationScriptSync: on uninitialized bridge");

File diff suppressed because it is too large Load Diff

View File

@ -32,7 +32,11 @@ public:
virtual ~JSBigString() {} virtual ~JSBigString() {}
virtual bool isAscii() const = 0; virtual bool isAscii() const = 0;
// This needs to be a \0 terminated string
virtual const char* c_str() const = 0; virtual const char* c_str() const = 0;
// Length of the c_str without the NULL byte.
virtual size_t size() const = 0; virtual size_t size() const = 0;
}; };