Skip to content

Commit

Permalink
add shared option to import memory
Browse files Browse the repository at this point in the history
  • Loading branch information
tonyfettes committed Dec 25, 2024
1 parent e628c26 commit 899325f
Show file tree
Hide file tree
Showing 5 changed files with 215 additions and 0 deletions.
19 changes: 19 additions & 0 deletions crates/moonbuild/src/gen/gen_build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -343,6 +343,8 @@ pub fn gen_link_command(
let export_memory_name = item.export_memory_name(moonc_opt.link_opt.target_backend);
let heap_start_address = item.heap_start_address(moonc_opt.link_opt.target_backend);
let import_memory = item.import_memory(moonc_opt.link_opt.target_backend);
let memory_limits = item.memory_limits(moonc_opt.link_opt.target_backend);
let shared_memory = item.shared_memory(moonc_opt.link_opt.target_backend);
let link_flags = item.link_flags(moonc_opt.link_opt.target_backend);

let native_cc = item.native_cc(moonc_opt.link_opt.target_backend);
Expand Down Expand Up @@ -418,6 +420,23 @@ pub fn gen_link_command(
im.name.clone(),
]
})
.lazy_args_with_cond(memory_limits.is_some(), || {
let ml = memory_limits.unwrap();
vec![
"-memory-limits-min".to_string(),
ml.min.to_string(),
"-memory-limits-max".to_string(),
ml.max.to_string(),
]
})
.lazy_args_with_cond(shared_memory.is_some(), || {
let sm = shared_memory.unwrap_or(false);
let mut args = vec![];
if sm {
args.push("-shared-memory".to_string())
}
args
})
.lazy_args_with_cond(heap_start_address.is_some(), || {
vec![
"-heap-start-address".to_string(),
Expand Down
51 changes: 51 additions & 0 deletions crates/moonbuild/template/pkg.schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -353,6 +353,22 @@
"null"
]
},
"memory-limits": {
"anyOf": [
{
"$ref": "#/definitions/memory-limits"
},
{
"type": "null"
}
]
},
"shared-memory": {
"type": [
"boolean",
"null"
]
},
"use-js-builtin-string": {
"type": [
"boolean",
Expand Down Expand Up @@ -405,6 +421,22 @@
"type": "null"
}
]
},
"memory-limits": {
"anyOf": [
{
"$ref": "#/definitions/memory-limits"
},
{
"type": "null"
}
]
},
"shared-memory": {
"type": [
"boolean",
"null"
]
}
}
},
Expand All @@ -422,6 +454,25 @@
"type": "string"
}
}
},
"memory-limits": {
"type": "object",
"required": [
"max",
"min"
],
"properties": {
"max": {
"type": "integer",
"format": "uint32",
"minimum": 0.0
},
"min": {
"type": "integer",
"format": "uint32",
"minimum": 0.0
}
}
}
}
}
43 changes: 43 additions & 0 deletions crates/moonutil/src/package.rs
Original file line number Diff line number Diff line change
Expand Up @@ -259,6 +259,13 @@ pub struct ImportMemory {
pub name: String,
}

#[derive(Debug, Serialize, Deserialize, Clone, JsonSchema)]
#[schemars(rename = "memory-limits")]
pub struct MemoryLimits {
pub min: u32,
pub max: u32,
}

#[derive(Debug, Clone)]
pub struct LinkDepItem {
pub out: String,
Expand All @@ -276,12 +283,16 @@ impl LinkDepItem {
pub fn wasm_exports(&self) -> Option<&[String]> { self.link.as_ref()?.wasm.as_ref()?.exports.as_deref() }
pub fn wasm_export_memory_name(&self) -> Option<&str> { self.link.as_ref()?.wasm.as_ref()?.export_memory_name.as_deref() }
pub fn wasm_import_memory(&self) -> Option<&ImportMemory> { self.link.as_ref()?.wasm.as_ref()?.import_memory.as_ref() }
pub fn wasm_memory_limits(&self) -> Option<&MemoryLimits> { self.link.as_ref()?.wasm.as_ref()?.memory_limits.as_ref() }
pub fn wasm_shared_memory(&self) -> Option<bool> { self.link.as_ref()?.wasm.as_ref()?.shared_memory }
pub fn wasm_heap_start_address(&self) -> Option<u32> { self.link.as_ref()?.wasm.as_ref()?.heap_start_address }
pub fn wasm_link_flags(&self) -> Option<&[String]> { self.link.as_ref()?.wasm.as_ref()?.flags.as_deref() }

pub fn wasm_gc_exports(&self) -> Option<&[String]> { self.link.as_ref()?.wasm_gc.as_ref()?.exports.as_deref() }
pub fn wasm_gc_export_memory_name(&self) -> Option<&str> { self.link.as_ref()?.wasm_gc.as_ref()?.export_memory_name.as_deref() }
pub fn wasm_gc_import_memory(&self) -> Option<&ImportMemory> { self.link.as_ref()?.wasm_gc.as_ref()?.import_memory.as_ref() }
pub fn wasm_gc_memory_limits(&self) -> Option<&MemoryLimits> { self.link.as_ref()?.wasm.as_ref()?.memory_limits.as_ref() }
pub fn wasm_gc_shared_memory(&self) -> Option<bool> { self.link.as_ref()?.wasm.as_ref()?.shared_memory }
pub fn wasm_gc_link_flags(&self) -> Option<&[String]> { self.link.as_ref()?.wasm_gc.as_ref()?.flags.as_deref() }

pub fn js_exports(&self) -> Option<&[String]> { self.link.as_ref()?.js.as_ref()?.exports.as_deref() }
Expand Down Expand Up @@ -322,6 +333,24 @@ impl LinkDepItem {
}
}

pub fn memory_limits(&self, b:TargetBackend) -> Option<&MemoryLimits> {
match b {
Wasm => self.wasm_memory_limits(),
WasmGC => self.wasm_gc_memory_limits(),
Js => None,
Native => None,
}
}

pub fn shared_memory(&self, b: TargetBackend) -> Option<bool> {
match b {
Wasm => self.wasm_shared_memory(),
WasmGC => self.wasm_gc_shared_memory(),
Js => None,
Native => None,
}
}

pub fn link_flags(&self, b: TargetBackend) -> Option<&[String]> {
match b {
Wasm => self.wasm_link_flags(),
Expand Down Expand Up @@ -366,6 +395,14 @@ pub struct WasmLinkConfig {
#[serde(rename = "import-memory")]
pub import_memory: Option<ImportMemory>,

#[serde(skip_serializing_if = "Option::is_none")]
#[serde(rename = "memory-limits")]
pub memory_limits: Option<MemoryLimits>,

#[serde(skip_serializing_if = "Option::is_none")]
#[serde(rename = "shared-memory")]
pub shared_memory: Option<bool>,

#[serde(skip_serializing_if = "Option::is_none")]
#[serde(rename = "export-memory-name")]
pub export_memory_name: Option<String>,
Expand Down Expand Up @@ -394,6 +431,12 @@ pub struct WasmGcLinkConfig {
#[serde(skip_serializing_if = "Option::is_none")]
pub import_memory: Option<ImportMemory>,

#[serde(skip_serializing_if = "Option::is_none")]
pub memory_limits: Option<MemoryLimits>,

#[serde(skip_serializing_if = "Option::is_none")]
pub shared_memory: Option<bool>,

#[serde(skip_serializing_if = "Option::is_none")]
pub export_memory_name: Option<String>,

Expand Down
51 changes: 51 additions & 0 deletions docs/manual-zh/src/source/pkg_json_schema.html
Original file line number Diff line number Diff line change
Expand Up @@ -391,6 +391,22 @@
"null"
]
},
"memory-limits": {
"anyOf": [
{
"$ref": "#/definitions/memory-limits"
},
{
"type": "null"
}
]
},
"shared-memory": {
"type": [
"boolean",
"null"
]
},
"use-js-builtin-string": {
"type": [
"boolean",
Expand Down Expand Up @@ -443,6 +459,22 @@
"type": "null"
}
]
},
"memory-limits": {
"anyOf": [
{
"$ref": "#/definitions/memory-limits"
},
{
"type": "null"
}
]
},
"shared-memory": {
"type": [
"boolean",
"null"
]
}
}
},
Expand All @@ -460,6 +492,25 @@
"type": "string"
}
}
},
"memory-limits": {
"type": "object",
"required": [
"max",
"min"
],
"properties": {
"max": {
"type": "integer",
"format": "uint32",
"minimum": 0.0
},
"min": {
"type": "integer",
"format": "uint32",
"minimum": 0.0
}
}
}
}
}
Expand Down
51 changes: 51 additions & 0 deletions docs/manual/src/source/pkg_json_schema.html
Original file line number Diff line number Diff line change
Expand Up @@ -391,6 +391,22 @@
"null"
]
},
"memory-limits": {
"anyOf": [
{
"$ref": "#/definitions/memory-limits"
},
{
"type": "null"
}
]
},
"shared-memory": {
"type": [
"boolean",
"null"
]
},
"use-js-builtin-string": {
"type": [
"boolean",
Expand Down Expand Up @@ -443,6 +459,22 @@
"type": "null"
}
]
},
"memory-limits": {
"anyOf": [
{
"$ref": "#/definitions/memory-limits"
},
{
"type": "null"
}
]
},
"shared-memory": {
"type": [
"boolean",
"null"
]
}
}
},
Expand All @@ -460,6 +492,25 @@
"type": "string"
}
}
},
"memory-limits": {
"type": "object",
"required": [
"max",
"min"
],
"properties": {
"max": {
"type": "integer",
"format": "uint32",
"minimum": 0.0
},
"min": {
"type": "integer",
"format": "uint32",
"minimum": 0.0
}
}
}
}
}
Expand Down

0 comments on commit 899325f

Please sign in to comment.