| 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 2023 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 "wasm/WasmStaticTypeDefs.h" |
| 20 |
|
| 21 |
#include "wasm/WasmTypeDef.h" |
| 22 |
|
| 23 |
using namespace js; |
| 24 |
using namespace js::wasm; |
| 25 |
|
| 26 |
const TypeDef* StaticTypeDefs::arrayMutI16 = nullptr; |
| 27 |
const TypeDef* StaticTypeDefs::jsTag = nullptr; |
| 28 |
|
| 29 |
bool StaticTypeDefs::init() { |
| 30 |
RefPtr<TypeContext> types = js_new<TypeContext>(); |
| 31 |
if (!types) { |
| 32 |
return false; |
| 33 |
} |
| 34 |
|
| 35 |
arrayMutI16 = types->addType(ArrayType(StorageType::I16, true)); |
| 36 |
if (!arrayMutI16) { |
| 37 |
return false; |
| 38 |
} |
| 39 |
arrayMutI16->recGroup().AddRef(); |
| 40 |
|
| 41 |
ValTypeVector params; |
| 42 |
if (!params.append(ValType(RefType::extern_()))) { |
| 43 |
return false; |
| 44 |
} |
| 45 |
jsTag = types->addType(FuncType(std::move(params), ValTypeVector())); |
| 46 |
if (!jsTag) { |
| 47 |
return false; |
| 48 |
} |
| 49 |
jsTag->recGroup().AddRef(); |
| 50 |
|
| 51 |
return true; |
| 52 |
} |
| 53 |
|
| 54 |
void StaticTypeDefs::destroy() { |
| 55 |
if (arrayMutI16) { |
| 56 |
arrayMutI16->recGroup().Release(); |
| 57 |
arrayMutI16 = nullptr; |
| 58 |
} |
| 59 |
if (jsTag) { |
| 60 |
jsTag->recGroup().Release(); |
| 61 |
jsTag = nullptr; |
| 62 |
} |
| 63 |
} |
| 64 |
|
| 65 |
bool StaticTypeDefs::addAllToTypeContext(TypeContext* types) { |
| 66 |
for (const TypeDef* type : {arrayMutI16, jsTag}) { |
| 67 |
MOZ_ASSERT(type, "static TypeDef was not initialized"); |
| 68 |
SharedRecGroup recGroup = &type->recGroup(); |
| 69 |
MOZ_ASSERT(recGroup->numTypes() == 1); |
| 70 |
if (!types->addRecGroup(recGroup)) { |
| 71 |
return false; |
| 72 |
} |
| 73 |
} |
| 74 |
return true; |
| 75 |
} |