Line data Source code
1 : /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 4 -*-
2 : * vim: set ts=8 sts=4 et sw=4 tw=99:
3 : *
4 : * Copyright 2016 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 : #ifndef wasm_text_utils
20 : #define wasm_text_utils
21 :
22 : #include "NamespaceImports.h"
23 :
24 : #include "vm/StringBuffer.h"
25 :
26 : namespace js {
27 : namespace wasm {
28 :
29 : template<size_t base>
30 : MOZ_MUST_USE bool
31 : RenderInBase(StringBuffer& sb, uint64_t num);
32 :
33 : template<class T>
34 : MOZ_MUST_USE bool
35 : RenderNaN(StringBuffer& sb, T num);
36 :
37 : // Helper class, StringBuffer wrapper, to track the position (line and column)
38 : // within the generated source.
39 :
40 : class WasmPrintBuffer
41 : {
42 : StringBuffer& stringBuffer_;
43 : uint32_t lineno_;
44 : uint32_t column_;
45 :
46 : public:
47 0 : explicit WasmPrintBuffer(StringBuffer& stringBuffer)
48 0 : : stringBuffer_(stringBuffer),
49 : lineno_(1),
50 0 : column_(1)
51 0 : {}
52 0 : inline char processChar(char ch) {
53 0 : if (ch == '\n') {
54 0 : lineno_++; column_ = 1;
55 : } else
56 0 : column_++;
57 0 : return ch;
58 : }
59 0 : inline char16_t processChar(char16_t ch) {
60 0 : if (ch == '\n') {
61 0 : lineno_++; column_ = 1;
62 : } else
63 0 : column_++;
64 0 : return ch;
65 : }
66 0 : bool append(const char ch) {
67 0 : return stringBuffer_.append(processChar(ch));
68 : }
69 0 : bool append(const char16_t ch) {
70 0 : return stringBuffer_.append(processChar(ch));
71 : }
72 0 : bool append(const char* str, size_t length) {
73 0 : for (size_t i = 0; i < length; i++)
74 0 : processChar(str[i]);
75 0 : return stringBuffer_.append(str, length);
76 : }
77 0 : bool append(const char16_t* begin, const char16_t* end) {
78 0 : for (const char16_t* p = begin; p != end; p++)
79 0 : processChar(*p);
80 0 : return stringBuffer_.append(begin, end);
81 : }
82 : bool append(const char16_t* str, size_t length) {
83 : return append(str, str + length);
84 : }
85 : template <size_t ArrayLength>
86 0 : bool append(const char (&array)[ArrayLength]) {
87 : static_assert(ArrayLength > 0, "null-terminated");
88 0 : MOZ_ASSERT(array[ArrayLength - 1] == '\0');
89 0 : return append(array, ArrayLength - 1);
90 : }
91 0 : char16_t getChar(size_t index) {
92 0 : return stringBuffer_.getChar(index);
93 : }
94 0 : size_t length() {
95 0 : return stringBuffer_.length();
96 : }
97 0 : StringBuffer& stringBuffer() { return stringBuffer_; }
98 0 : uint32_t lineno() { return lineno_; }
99 0 : uint32_t column() { return column_; }
100 : };
101 :
102 : } // namespace wasm
103 : } // namespace js
104 :
105 : #endif // namespace wasm_text_utils
|