summaryrefslogtreecommitdiff
path: root/src/support/base64.h
diff options
context:
space:
mode:
authorAlon Zakai <alonzakai@gmail.com>2019-04-11 14:01:51 -0700
committerGitHub <noreply@github.com>2019-04-11 14:01:51 -0700
commitb769b4ede65eb014376b67f78ba5e6cb04e0cef8 (patch)
treef0e4925b2d2e3328750c3e7d0190b0801f211faf /src/support/base64.h
parent4905c3d3b4be20a89920ea2a56c1868294e77b65 (diff)
downloadbinaryen-b769b4ede65eb014376b67f78ba5e6cb04e0cef8.tar.gz
binaryen-b769b4ede65eb014376b67f78ba5e6cb04e0cef8.tar.bz2
binaryen-b769b4ede65eb014376b67f78ba5e6cb04e0cef8.zip
Wasm2js refactoring (#1997)
Early work for #1929 * Leave core wasm module - the "asm.js function" - to Wasm2JSBuilder, and add Wasm2JSGlue which emits the code before and after that. Currently that's some ES6 code, but we may want to change that later. * Add add AssertionEmitter class for the sole purpose of emitting modules + assertions for testing. This avoids some hacks from before like starting from index 1 (assuming the module at first position was already parsed and printed) and printing of the f32Equal etc. functions not at the very top (which was due to technical limitations before). Logic-wise, there should be no visible change, except some whitespace and reodering, and that I made the exceptions print out the source of the assertion that failed from the wast: -if (!check2()) fail2(); +if (!check2()) throw 'assertion failed: ( assert_return ( call add ( i32.const 1 ) ( i32.const 1 ) ) ( i32.const 2 ) )'; (fail2 etc. did not exist, and seems to just have given a unique number for each assertion?)
Diffstat (limited to 'src/support/base64.h')
-rw-r--r--src/support/base64.h66
1 files changed, 66 insertions, 0 deletions
diff --git a/src/support/base64.h b/src/support/base64.h
new file mode 100644
index 000000000..0c87c37c1
--- /dev/null
+++ b/src/support/base64.h
@@ -0,0 +1,66 @@
+/*
+ * Copyright 2019 WebAssembly Community Group participants
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#ifndef wasm_support_base64_h
+#define wasm_support_base64_h
+
+#include <cassert>
+#include <string>
+#include <vector>
+
+inline std::string base64Encode(std::vector<char> &data) {
+ std::string ret;
+ size_t i = 0;
+
+ const char* alphabet =
+ "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
+ "abcdefghijklmnopqrstuvwxyz"
+ "0123456789+/";
+
+ while (i + 3 <= data.size()) {
+ uint32_t bits =
+ (((uint32_t)(uint8_t) data[i + 0]) << 16) |
+ (((uint32_t)(uint8_t) data[i + 1]) << 8) |
+ (((uint32_t)(uint8_t) data[i + 2]) << 0);
+ ret += alphabet[(bits >> 18) & 0x3f];
+ ret += alphabet[(bits >> 12) & 0x3f];
+ ret += alphabet[(bits >> 6) & 0x3f];
+ ret += alphabet[(bits >> 0) & 0x3f];
+ i += 3;
+ }
+
+ if (i + 2 == data.size()) {
+ uint32_t bits =
+ (((uint32_t)(uint8_t) data[i + 0]) << 8) |
+ (((uint32_t)(uint8_t) data[i + 1]) << 0);
+ ret += alphabet[(bits >> 10) & 0x3f];
+ ret += alphabet[(bits >> 4) & 0x3f];
+ ret += alphabet[(bits << 2) & 0x3f];
+ ret += '=';
+ } else if (i + 1 == data.size()) {
+ uint32_t bits = (uint32_t)(uint8_t) data[i + 0];
+ ret += alphabet[(bits >> 2) & 0x3f];
+ ret += alphabet[(bits << 4) & 0x3f];
+ ret += '=';
+ ret += '=';
+ } else {
+ assert(i == data.size());
+ }
+
+ return ret;
+}
+
+#endif // wasm_support_base64_h