| 1 |
/* Copyright 2018 Mozilla Foundation |
| 2 |
* |
| 3 |
* Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 |
* you may not use this file except in compliance with the License. |
| 5 |
* You may obtain a copy of the License at |
| 6 |
* |
| 7 |
* http://www.apache.org/licenses/LICENSE-2.0 |
| 8 |
* |
| 9 |
* Unless required by applicable law or agreed to in writing, software |
| 10 |
* distributed under the License is distributed on an "AS IS" BASIS, |
| 11 |
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 |
* See the License for the specific language governing permissions and |
| 13 |
* limitations under the License. |
| 14 |
*/ |
| 15 |
|
| 16 |
use crate::{BinaryReader, ConstExpr, FromReader, GlobalType, Result, SectionLimited}; |
| 17 |
|
| 18 |
/// Represents a core WebAssembly global. |
| 19 |
#[derive(Debug, Clone)] |
| 20 |
pub struct Global<'a> { |
| 21 |
/// The global's type. |
| 22 |
pub ty: GlobalType, |
| 23 |
/// The global's initialization expression. |
| 24 |
pub init_expr: ConstExpr<'a>, |
| 25 |
} |
| 26 |
|
| 27 |
/// A reader for the global section of a WebAssembly module. |
| 28 |
pub type GlobalSectionReader<'a> = SectionLimited<'a, Global<'a>>; |
| 29 |
|
| 30 |
impl<'a> FromReader<'a> for Global<'a> { |
| 31 |
fn from_reader(reader: &mut BinaryReader<'a>) -> Result<Self> { |
| 32 |
let ty = reader.read()?; |
| 33 |
let init_expr = reader.read()?; |
| 34 |
Ok(Global { ty, init_expr }) |
| 35 |
} |
| 36 |
} |
| 37 |
|
| 38 |
impl<'a> FromReader<'a> for GlobalType { |
| 39 |
fn from_reader(reader: &mut BinaryReader<'a>) -> Result<Self> { |
| 40 |
let content_type = reader.read()?; |
| 41 |
let flags = reader.read_u8()?; |
| 42 |
if reader.shared_everything_threads() { |
| 43 |
if flags > 0b11 { |
| 44 |
bail!(reader.original_position() - 1, "malformed global flags") |
| 45 |
} |
| 46 |
} else { |
| 47 |
if flags > 0b1 { |
| 48 |
bail!( |
| 49 |
reader.original_position() - 1, |
| 50 |
"malformed mutability -- or shared globals \ |
| 51 |
require the shared-everything-threads proposal" |
| 52 |
) |
| 53 |
} |
| 54 |
} |
| 55 |
Ok(GlobalType { |
| 56 |
content_type, |
| 57 |
mutable: (flags & 0b01) > 0, |
| 58 |
shared: (flags & 0b10) > 0, |
| 59 |
}) |
| 60 |
} |
| 61 |
} |