Skip to content

Commit

Permalink
build: add support for generating a base64-encoded Wasm module string
Browse files Browse the repository at this point in the history
  • Loading branch information
kgryte committed Sep 19, 2024
1 parent 990ffea commit 6e5b8a1
Show file tree
Hide file tree
Showing 5 changed files with 152 additions and 1 deletion.
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
/**
* @license Apache-2.0
*
* Copyright (c) 2024 The Stdlib Authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

'use strict';

// MODULES //

var base64ToUint8Array = require( '@stdlib/string/base/base64-to-uint8array' );


// MAIN //

var wasm = base64ToUint8Array( 'AGFzbQEAAAAADwhkeWxpbmsuMAEEAAAAAAEYA2AAAGAGf3x/f39/AGAIf3x/f39/f38AAg8BA2VudgZtZW1vcnkCAAADBAMAAQIHTAQRX193YXNtX2NhbGxfY3RvcnMAABhfX3dhc21fYXBwbHlfZGF0YV9yZWxvY3MAAAdjX2RheHB5AAEPY19kYXhweV9uZGFycmF5AAIKmAIDAwABCz0BAn4gACABIAIgAyADrCIGQgEgAKwiB31+QgAgBkIAVxunIAQgBSAFrCIGQgEgB31+QgAgBkIAVxunEAIL0wEBBX8CQCAAQQBMDQAgAUQAAAAAAAAAAGENACAAQQFxIQkgAEEBRwRAIABB/v///wdxIQogBiAGaiELIAMgA2ohDEEAIQADQCAFIAdBA3RqIgggASACIARBA3RqKwMAoiAIKwMAoDkDACAFIAYgB2pBA3RqIgggASACIAMgBGpBA3RqKwMAoiAIKwMAoDkDACAHIAtqIQcgBCAMaiEEIABBAmoiACAKRw0ACwsgCUUNACAFIAdBA3RqIgAgASACIARBA3RqKwMAoiAAKwMAoDkDAAsL' );


// EXPORTS //

module.exports = wasm;
3 changes: 3 additions & 0 deletions lib/node_modules/@stdlib/blas/base/daxpy-wasm/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,9 @@
}
],
"main": "./lib",
"browser": {
"./lib/binary.js": "./lib/binary.browser.js"
},
"directories": {
"benchmark": "./benchmark",
"doc": "./docs",
Expand Down
63 changes: 63 additions & 0 deletions lib/node_modules/@stdlib/blas/base/daxpy-wasm/scripts/build.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
#!/usr/bin/env node

/**
* @license Apache-2.0
*
* Copyright (c) 2024 The Stdlib Authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

'use strict';

// MODULES //

var resolve = require( 'path' ).resolve;
var readFile = require( '@stdlib/fs/read-file' ).sync;
var writeFile = require( '@stdlib/fs/write-file' ).sync;
var replace = require( '@stdlib/string/replace' );


// VARIABLES //

var wpath = resolve( __dirname, '..', 'src', 'main.wasm' );
var tpath = resolve( __dirname, 'template.txt' );
var opath = resolve( __dirname, '..', 'lib', 'binary.browser.js' );

var opts = {
'encoding': 'utf8'
};

var PLACEHOLDER = '{{WASM_BASE64}}';


// MAIN //

/**
* Main execution sequence.
*
* @private
*/
function main() {
var wasm;
var tmpl;

wasm = readFile( wpath );
tmpl = readFile( tpath, opts );

tmpl = replace( tmpl, PLACEHOLDER, wasm.toString( 'base64' ) );

writeFile( opath, tmpl, opts );
}

main();
33 changes: 33 additions & 0 deletions lib/node_modules/@stdlib/blas/base/daxpy-wasm/scripts/template.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
/**
* @license Apache-2.0
*
* Copyright (c) 2024 The Stdlib Authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

'use strict';

// MODULES //

var base64ToUint8Array = require( '@stdlib/string/base/base64-to-uint8array' );


// MAIN //

var wasm = base64ToUint8Array( '{{WASM_BASE64}}' );


// EXPORTS //

module.exports = wasm;
21 changes: 20 additions & 1 deletion lib/node_modules/@stdlib/blas/base/daxpy-wasm/src/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,13 @@ else
WASM_TO_JS := wasm2js
endif

# Define the path to the Node.js executable:
ifdef NODE
NODEJS := $(NODE)
else
NODEJS := node
endif

# Define the command-line options when compiling C files:
CFLAGS ?= \
-std=c99 \
Expand Down Expand Up @@ -115,6 +122,9 @@ wat_targets := main.wat
# List of WebAssembly JavaScript targets:
wasm_js_targets := main.wasm.js

# List of other JavaScript targets:
browser_js_targets := ./../lib/binary.browser.js


# RULES #

Expand Down Expand Up @@ -155,7 +165,7 @@ all: wasm
# @example
# make wasm
#/
wasm: $(wasm_targets) $(wat_targets)
wasm: $(wasm_targets) $(wat_targets) $(browser_js_targets)

.PHONY: wasm

Expand Down Expand Up @@ -191,6 +201,15 @@ $(wat_targets): %.wat: %.wasm
$(wasm_js_targets): %.wasm.js: %.wasm
$(QUIET) $(WASM_TO_JS) -o $@ $(wasm_targets)

#/
# Generates an inline WebAssembly build for use in bundlers.
#
# @private
# @param {string} NODE - Node.js executable
#/
$(browser_js_targets): $(wasm_targets)
$(QUIET) $(NODEJS) ./../scripts/build.js

#/
# Removes generated WebAssembly files.
#
Expand Down

0 comments on commit 6e5b8a1

Please sign in to comment.