Skip to content

Commit 93a764c

Browse files
committed
feat!: Move To Grain V0.6.0
1 parent 9701e89 commit 93a764c

12 files changed

+151
-111
lines changed

ReadMe.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
> **Note**
2-
> This Linker Requires A Version Of Grain Greater Than Or Equal To v0.5.5
2+
> This Linker Requires A Version Of Grain Greater Than Or Equal To v0.6.0
33
44
# Brisk Linker
55
This program is a general purpose wasm linker built for the Brisk programming language but open enough to be used by other languages compiled to wasm.

index.gr

+17-9
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,16 @@
1+
module Main
2+
13
// Imports
2-
import Bytes from "bytes"
3-
import File from "sys/file"
4-
import Process from "sys/process"
5-
import Int64 from "int64"
6-
import String from "string"
7-
import { resolvePath } from "./linker/ResolvePath"
8-
import { link } from "./linker/index"
4+
include "bytes"
5+
include "sys/file"
6+
include "sys/process"
7+
include "int64"
8+
include "string"
9+
include "./linker/ResolvePath"
10+
include "./linker/index"
11+
from ResolvePath use { resolvePath }
12+
from Linker use { link }
13+
914
record Arguments {
1015
inFile: Option<String>,
1116
outFile: Option<String>,
@@ -27,7 +32,10 @@ let writeFile = (filePath, fileContents) => {
2732
match (fd) {
2833
Ok(descriptor) => {
2934
// Write The File Contents
30-
let writeSuccess = File.fdWrite(descriptor, fileContents)
35+
let writeSuccess = File.fdWrite(
36+
descriptor,
37+
Bytes.fromString(fileContents)
38+
)
3139
match (writeSuccess) {
3240
Ok(_) => void,
3341
Err(_) => fail "[Error Writing File: " ++ filePath ++ "]",
@@ -54,7 +62,7 @@ let readFile = filePath => {
5462
match (File.fdFilestats(descriptor)) {
5563
Ok(stats) =>
5664
match (File.fdRead(descriptor, Int64.toNumber(stats.size))) {
57-
Ok((contents, _)) => Ok(Bytes.fromString(contents)),
65+
Ok((contents, _)) => Ok(contents),
5866
Err(err) => throw err,
5967
},
6068
Err(err) => throw err,

linker/BuildFile.gr

+31-27
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,22 @@
1+
module Builder
2+
13
// Imports
2-
import Buffer from "buffer"
3-
import List from "list"
4-
import Map from "map"
5-
import Int32 from "int32"
6-
import Stream from "./Stream"
7-
import DecodeSection, { getSectionStream } from "./Decoder/DecodeSection"
8-
import { decodeCount } from "./DecodeHelpers"
9-
import WasmDecoder from "./Decoder/WasmDecoder"
10-
import Encoder from "./encode"
11-
import { impossibleErr } from "./Errors"
12-
// Type Imports
13-
import DecodeFile from "./Decoder/DecodeFile"
4+
include "buffer"
5+
include "list"
6+
include "map"
7+
include "int32"
8+
include "./Stream"
9+
include "./Decoder/decodeSection"
10+
include "./Decoder/DecodeFile"
11+
include "./DecodeHelpers"
12+
include "./Decoder/WasmDecoder"
13+
include "./Encoder"
14+
include "./Errors"
15+
from DecodeSection use { getSectionStream }
16+
from DecodeHelpers use { decodeCount }
17+
from Errors use { impossibleErr }
1418
// WasmBinary Type
15-
record WasmBinary {
19+
provide record WasmBinary {
1620
// Naming Data
1721
mut moduleName: String,
1822
mut funcNames: List<(Number, String)>,
@@ -35,7 +39,7 @@ record WasmBinary {
3539
mut dataSection: List<Buffer.Buffer>, // dataSection[]
3640
}
3741
// Types
38-
record WasmData {
42+
provide record WasmData {
3943
mut globalCount: Number,
4044
mut typeCount: Number,
4145
mut funcCount: Number,
@@ -51,7 +55,7 @@ let listToBuffer = byteList => {
5155
buffer
5256
}
5357
// Create The Sections
54-
export let createCustomSection = (wasmBinary, dependencyChain, wasmData) => {
58+
provide let createCustomSection = (wasmBinary, dependencyChain, wasmData) => {
5559
List.forEachi((dependency: DecodeFile.FileData, index) => {
5660
// Get The Custom Sections
5761
List.forEach(wasmSection => {
@@ -176,7 +180,7 @@ export let createCustomSection = (wasmBinary, dependencyChain, wasmData) => {
176180
}, dependency.wasmSections)
177181
}, dependencyChain)
178182
}
179-
export let createTypeSection = (wasmBinary, dependencyChain, wasmData) => {
183+
provide let createTypeSection = (wasmBinary, dependencyChain, wasmData) => {
180184
List.forEach((dependency: DecodeFile.FileData) => {
181185
// TODO: Consider Merging Duplicate Types
182186
// Get The Type Section
@@ -195,7 +199,7 @@ export let createTypeSection = (wasmBinary, dependencyChain, wasmData) => {
195199
}
196200
}, dependencyChain)
197201
}
198-
export let createImportSection = (wasmBinary, dependencyChain, wasmData) => {
202+
provide let createImportSection = (wasmBinary, dependencyChain, wasmData) => {
199203
List.forEach((dependency: DecodeFile.FileData) => {
200204
// Create local Counts
201205
let mut localFunctionCount = 0
@@ -264,7 +268,7 @@ export let createImportSection = (wasmBinary, dependencyChain, wasmData) => {
264268
}, List.reverse(dependency.wasmImports))
265269
}, List.reverse(dependencyChain))
266270
}
267-
export let createFuncSection = (wasmBinary, dependencyChain, wasmData) => {
271+
provide let createFuncSection = (wasmBinary, dependencyChain, wasmData) => {
268272
List.forEach((dependency: DecodeFile.FileData) => {
269273
// Get The Function Section
270274
let funcStream = getSectionStream(0x03, dependency.wasmSections)
@@ -336,7 +340,7 @@ export let createFuncSection = (wasmBinary, dependencyChain, wasmData) => {
336340
wasmBinary.funcSection = List.append(wasmBinary.funcSection, functions)
337341
}, dependencyChain)
338342
}
339-
export let createTableSection = (wasmBinary, dependencyChain, wasmData) => {
343+
provide let createTableSection = (wasmBinary, dependencyChain, wasmData) => {
340344
// TODO: Once wasm supports multiple tables allocate a table per dependency
341345
let mut initialTableSize = 0
342346
List.forEach((dependency: DecodeFile.FileData) => {
@@ -352,7 +356,7 @@ export let createTableSection = (wasmBinary, dependencyChain, wasmData) => {
352356
)
353357
wasmBinary.tableSection = [table, ...wasmBinary.tableSection]
354358
}
355-
export let createMemorySection = (wasmBinary, dependencyChain, wasmData) => {
359+
provide let createMemorySection = (wasmBinary, dependencyChain, wasmData) => {
356360
// TODO: Once wasm supports multiple memories consider using a separate memory per dependency
357361
if (!wasmBinary.hasMemoryImport) {
358362
let mut initialMemorySize = 0
@@ -369,7 +373,7 @@ export let createMemorySection = (wasmBinary, dependencyChain, wasmData) => {
369373
]
370374
}
371375
}
372-
export let createGlobalSection = (wasmBinary, dependencyChain, wasmData) => {
376+
provide let createGlobalSection = (wasmBinary, dependencyChain, wasmData) => {
373377
// Map Deps
374378
List.forEach((dependency: DecodeFile.FileData) => {
375379
// Get The Type Section
@@ -444,7 +448,7 @@ export let createGlobalSection = (wasmBinary, dependencyChain, wasmData) => {
444448
]
445449
}, dependencyChain)
446450
}
447-
export let createExportSection = (wasmBinary, dependencyChain, wasmData) => {
451+
provide let createExportSection = (wasmBinary, dependencyChain, wasmData) => {
448452
List.forEach((dependency: DecodeFile.FileData) => {
449453
List.forEach((wasmExport: WasmDecoder.WasmExport) => {
450454
if (!wasmExport.briskExport && dependency.isEntry) {
@@ -482,7 +486,7 @@ export let createExportSection = (wasmBinary, dependencyChain, wasmData) => {
482486
}, dependency.wasmExports)
483487
}, dependencyChain)
484488
}
485-
export let createStartSection = (wasmBinary, dependencyChain, wasmData) => {
489+
provide let createStartSection = (wasmBinary, dependencyChain, wasmData) => {
486490
// Create The Dependency Map
487491
let dependencyMap = Map.fromList(
488492
List.map((dep: DecodeFile.FileData) =>
@@ -624,7 +628,7 @@ export let createStartSection = (wasmBinary, dependencyChain, wasmData) => {
624628
// Export Start
625629
wasmBinary.exportSection = [startExport, ...wasmBinary.exportSection]
626630
}
627-
export let createElementSection = (wasmBinary, dependencyChain, wasmData) => {
631+
provide let createElementSection = (wasmBinary, dependencyChain, wasmData) => {
628632
let mut elementOffset = 0
629633
List.forEach((dependency: DecodeFile.FileData) => {
630634
// Get The Element Section
@@ -675,14 +679,14 @@ export let createElementSection = (wasmBinary, dependencyChain, wasmData) => {
675679
})
676680
}, dependencyChain)
677681
}
678-
export let createDataSection = (wasmBinary, dependencyChain, wasmData) => {
682+
provide let createDataSection = (wasmBinary, dependencyChain, wasmData) => {
679683
// TODO: Create Data Section
680684
// TODO: Create Data Count Section
681685
// Return The Buffer
682686
wasmBinary
683687
}
684688
// Rebuild WasmBinary
685-
export let buildBinary = wasmBinaryContent => {
689+
provide let buildBinary = wasmBinaryContent => {
686690
// Helpers
687691
let encodeSection = (sectionID, sectionContent) => {
688692
let sectionBuffer = listToBuffer(
@@ -865,7 +869,7 @@ export let buildBinary = wasmBinaryContent => {
865869
wasmBinary
866870
}
867871
// Build The File
868-
export let buildFile = dependencyChain => {
872+
provide let buildFile = dependencyChain => {
869873
// Create Wasm Data
870874
let wasmData = { globalCount: 0, typeCount: 0, funcCount: 0 }
871875
// Create The Wasm Binary Content

linker/DecodeHelpers.gr

+6-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
1-
import Stream from "./Stream"
2-
export let decodeCount = (stream, decoder) => {
1+
module DecodeHelpers
2+
3+
// Import
4+
include "./stream"
5+
// Provides
6+
provide let decodeCount = (stream, decoder) => {
37
if (Stream.length(stream) > 0) {
48
let count = Stream.nextUIntLEB128(stream)
59
for (let mut i = 0; i < count; i += 1) {

linker/Decoder/DecodeFile.gr

+13-10
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,20 @@
1+
module DecodeFile
2+
13
// Imports
2-
import List from "list"
3-
import String from "string"
4-
import Stream from "../Stream"
5-
import Map from "map"
6-
import DecodeSection, {
4+
include "list"
5+
include "string"
6+
include "../stream"
7+
include "map"
8+
include "./DecodeSection"
9+
include "./WasmDecoder"
10+
from DecodeSection use {
711
decodeLinkingSection,
812
decodeFile,
913
decodeImportSection,
1014
decodeExportSection,
11-
} from "./DecodeSection"
12-
import WasmDecoder from "./WasmDecoder"
15+
}
1316
// Types
14-
export record OffsetData {
17+
provide record OffsetData {
1518
// Normal Offsets
1619
mut functionTableOffset: Number,
1720
// Import Counts
@@ -22,7 +25,7 @@ export record OffsetData {
2225
funcMap: Map.Map<Number, Number>,
2326
typeMap: Map.Map<Number, Number>,
2427
}
25-
export record FileData {
28+
provide record FileData {
2629
// Module Data
2730
modulePath: String,
2831
importIdentifier: String,
@@ -41,7 +44,7 @@ export record FileData {
4144
mut offsetData: OffsetData,
4245
}
4346
// Decodes A Wasm File
44-
export let decodeFile = (modulePath, wasmBinary) => {
47+
provide let decodeFile = (modulePath, wasmBinary) => {
4548
// Create Our Stream
4649
let stream = Stream.fromBytes(wasmBinary)
4750
// Ensure The File Is Valid

linker/Decoder/DecodeSection.gr

+16-13
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,12 @@
1+
module DecodeSection
2+
13
// Imports
2-
import List from "list"
3-
import Stream from "../Stream"
4-
import WasmDecoder, { decodeWasmImport, decodeWasmExport } from "./WasmDecoder"
4+
include "list"
5+
include "../stream"
6+
include "./WasmDecoder"
7+
from WasmDecoder use { decodeWasmImport, decodeWasmExport }
58
// Types
6-
export enum WasmSection {
9+
provide enum WasmSection {
710
// SectionType(Length, Body)
811
CustomSection(Stream.Stream),
912
TypeSection(Stream.Stream),
@@ -19,12 +22,12 @@ export enum WasmSection {
1922
DataSection(Stream.Stream),
2023
DataCountSection(Stream.Stream),
2124
}
22-
export enum CodeReference {
25+
provide enum CodeReference {
2326
FuncRef(Number),
2427
TypeRef(Number),
2528
GlobalRef(Number),
2629
}
27-
export record LinkingInfo {
30+
provide record LinkingInfo {
2831
// Language Import Identifier
2932
importIdentifier: String,
3033
// The file functionOffsetGlobal Index
@@ -33,7 +36,7 @@ export record LinkingInfo {
3336
codeReferences: List<List<CodeReference>>,
3437
}
3538
// Getters
36-
export let getSectionStream = (wasmSectionID, wasmSections) => {
39+
provide let getSectionStream = (wasmSectionID, wasmSections) => {
3740
// Get The Stream
3841
let mut sectionStream = Stream.empty()
3942
let setStream = stream => {
@@ -60,7 +63,7 @@ export let getSectionStream = (wasmSectionID, wasmSections) => {
6063
// Return The Stream
6164
sectionStream
6265
}
63-
export let getLinkingInfoStream = wasmSections => {
66+
provide let getLinkingInfoStream = wasmSections => {
6467
let linkingSection = List.find(section => match (section) {
6568
CustomSection(stream) when Stream.nextString(stream) == "LinkingInfo" =>
6669
true,
@@ -72,7 +75,7 @@ export let getLinkingInfoStream = wasmSections => {
7275
}
7376
}
7477
// Decoders
75-
export let decodeSection = wasmStream => {
78+
provide let decodeSection = wasmStream => {
7679
// Get Section Id
7780
let sectionID = Stream.nextByte(wasmStream)
7881
// Get The Section Length
@@ -97,7 +100,7 @@ export let decodeSection = wasmStream => {
97100
_ => fail "Invalid Section",
98101
}
99102
}
100-
export let decodeFile = wasmStream => {
103+
provide let decodeFile = wasmStream => {
101104
let mut sections = []
102105
while (Stream.remainingLength(wasmStream) > 0) {
103106
let section = decodeSection(wasmStream)
@@ -120,7 +123,7 @@ let getRefs = (wasmStream, refType) => {
120123
})
121124
}
122125
// Decode Sections
123-
export let decodeLinkingSection = wasmSections => {
126+
provide let decodeLinkingSection = wasmSections => {
124127
// Get LinkingInfoStream
125128
let linkingInfoStream = getLinkingInfoStream(wasmSections)
126129
// Parse Linking Info Section
@@ -148,7 +151,7 @@ export let decodeLinkingSection = wasmSections => {
148151
// Package
149152
{ importIdentifier, functionOffsetGlobal, codeReferences }
150153
}
151-
export let decodeImportSection = wasmSections => {
154+
provide let decodeImportSection = wasmSections => {
152155
// Find The Import Sections
153156
let wasmStream = getSectionStream(0x02, wasmSections)
154157
// Set The Stream Position To 0
@@ -160,7 +163,7 @@ export let decodeImportSection = wasmSections => {
160163
decodeWasmImport(wasmStream)
161164
})
162165
}
163-
export let decodeExportSection = wasmSections => {
166+
provide let decodeExportSection = wasmSections => {
164167
// Find The Export Sections
165168
let wasmStream = getSectionStream(0x07, wasmSections)
166169
// Set The Stream Position To 0

0 commit comments

Comments
 (0)