WasmTesting.cpp (1915B)
1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- 2 * vim: set ts=8 sts=2 et sw=2 tw=80: 3 * 4 * Copyright 2015 Mozilla Foundation 5 * 6 * Licensed under the Apache License, Version 2.0 (the "License"); 7 * you may not use this file except in compliance with the License. 8 * You may obtain a copy of the License at 9 * 10 * http://www.apache.org/licenses/LICENSE-2.0 11 * 12 * Unless required by applicable law or agreed to in writing, software 13 * distributed under the License is distributed on an "AS IS" BASIS, 14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 15 * See the License for the specific language governing permissions and 16 * limitations under the License. 17 */ 18 19 #include "shell/WasmTesting.h" 20 21 #include <inttypes.h> 22 #include <stdbool.h> 23 #include <stddef.h> 24 25 #include "js/Printf.h" 26 #include "wasm/WasmTypeDecls.h" 27 28 using namespace js; 29 using namespace js::wasm; 30 31 extern "C" { 32 bool wasm_text_to_binary(const char16_t* text, size_t text_len, 33 uint8_t** out_bytes, size_t* out_bytes_len, 34 uint8_t** out_error, size_t* out_error_len); 35 } // extern "C" 36 37 bool wasm::TextToBinary(const char16_t* text, size_t textLen, Bytes* bytes, 38 UniqueChars* error) { 39 uint8_t* outBytes = nullptr; 40 size_t outBytesLength = 0; 41 42 uint8_t* outError = nullptr; 43 size_t outErrorLength = 0; 44 45 bool result = wasm_text_to_binary(text, textLen, &outBytes, &outBytesLength, 46 &outError, &outErrorLength); 47 48 if (result) { 49 if (outBytesLength == 0) { 50 *error = JS_smprintf("missing bytes"); 51 return false; 52 } 53 54 MOZ_ASSERT(outBytes); 55 MOZ_ASSERT(outBytesLength > 0); 56 bytes->replaceRawBuffer(outBytes, outBytesLength); 57 return true; 58 } 59 60 MOZ_ASSERT(outError); 61 MOZ_ASSERT(outErrorLength > 0); 62 *error = UniqueChars{(char*)outError}; 63 return false; 64 }