2016-11-23 14:31:43 +00:00
|
|
|
// Copyright 2004-present Facebook. All Rights Reserved.
|
|
|
|
|
|
|
|
#pragma once
|
|
|
|
|
|
|
|
#include <cstdint>
|
|
|
|
#include <cstring>
|
|
|
|
|
2017-06-27 18:15:36 +00:00
|
|
|
#ifndef RN_EXPORT
|
|
|
|
#define RN_EXPORT __attribute__((visibility("default")))
|
|
|
|
#endif
|
|
|
|
|
2016-11-23 14:31:43 +00:00
|
|
|
namespace facebook {
|
|
|
|
namespace react {
|
|
|
|
|
|
|
|
/*
|
|
|
|
* ScriptTag
|
|
|
|
*
|
|
|
|
* Scripts given to the JS Executors to run could be in any of the following
|
|
|
|
* formats. They are tagged so the executor knows how to run them.
|
|
|
|
*/
|
|
|
|
enum struct ScriptTag {
|
|
|
|
String = 0,
|
|
|
|
RAMBundle,
|
|
|
|
BCBundle,
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* BundleHeader
|
|
|
|
*
|
|
|
|
* RAM bundles and BC bundles begin with headers. For RAM bundles this is
|
|
|
|
* 4 bytes, for BC bundles this is 12 bytes. This structure holds the first 12
|
|
|
|
* bytes from a bundle in a way that gives access to that information.
|
|
|
|
*/
|
Simplifying Struct definition.
Summary:
Since we are reading from a file, we should make sure this struct is packed, just in case we change it down the line and the compiler decides it might want to introduce padding, we're now protected against that.
There was also a discussion about the fact that people might use `ptr += sizeof(BundleHeader)` as an idiom in their code, which would currently be incorrect, if padding was introduced at the end of the file. Actually, it remains incorrect to do that now, because a RAM bundle header is a different size to a BC Bundle header. If people are properly testing their code, they should spot this pretty quickly, because it will always be an incorrect thing to do with a RAM bundle, so this isn't as bad as previously thought: where the code only succeeds when the compiler deigns to not pad the struct at the end.
This diff also cleans up how headers are initialised. `BundleHeader` has a constructor that explicitly zero-initialises it so we can rely on the default initializer to do the right thing now.
Reviewed By: mhorowitz
Differential Revision: D4572032
fbshipit-source-id: 7dc50cfa9438dfdfb9f842dc39d8f15334813c63
2017-02-20 12:28:32 +00:00
|
|
|
struct __attribute__((packed)) BundleHeader {
|
2016-11-23 14:31:43 +00:00
|
|
|
BundleHeader() {
|
|
|
|
std::memset(this, 0, sizeof(BundleHeader));
|
|
|
|
}
|
|
|
|
|
Simplifying Struct definition.
Summary:
Since we are reading from a file, we should make sure this struct is packed, just in case we change it down the line and the compiler decides it might want to introduce padding, we're now protected against that.
There was also a discussion about the fact that people might use `ptr += sizeof(BundleHeader)` as an idiom in their code, which would currently be incorrect, if padding was introduced at the end of the file. Actually, it remains incorrect to do that now, because a RAM bundle header is a different size to a BC Bundle header. If people are properly testing their code, they should spot this pretty quickly, because it will always be an incorrect thing to do with a RAM bundle, so this isn't as bad as previously thought: where the code only succeeds when the compiler deigns to not pad the struct at the end.
This diff also cleans up how headers are initialised. `BundleHeader` has a constructor that explicitly zero-initialises it so we can rely on the default initializer to do the right thing now.
Reviewed By: mhorowitz
Differential Revision: D4572032
fbshipit-source-id: 7dc50cfa9438dfdfb9f842dc39d8f15334813c63
2017-02-20 12:28:32 +00:00
|
|
|
uint32_t magic;
|
|
|
|
uint32_t reserved_;
|
|
|
|
uint32_t version;
|
2016-11-23 14:31:43 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* parseTypeFromHeader
|
|
|
|
*
|
|
|
|
* Takes the first 8 bytes of a bundle, and returns a tag describing the
|
|
|
|
* bundle's format.
|
|
|
|
*/
|
2017-06-27 18:15:36 +00:00
|
|
|
RN_EXPORT ScriptTag parseTypeFromHeader(const BundleHeader& header);
|
2016-11-23 14:31:43 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* stringForScriptTag
|
|
|
|
*
|
|
|
|
* Convert an `ScriptTag` enum into a string, useful for emitting in errors
|
|
|
|
* and diagnostic messages.
|
|
|
|
*/
|
|
|
|
const char* stringForScriptTag(const ScriptTag& tag);
|
|
|
|
|
|
|
|
} // namespace react
|
|
|
|
} // namespace facebook
|