Skip to content

Commit

Permalink
feat(runPipelineEmscripten): JsonObject input support
Browse files Browse the repository at this point in the history
  • Loading branch information
thewtex committed Jun 21, 2023
1 parent 9f541d1 commit e10cba1
Show file tree
Hide file tree
Showing 10 changed files with 115 additions and 8 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,8 @@ test/pipelines/mesh-read-write-pipeline/emscripten-build/
test/pipelines/stdout-stderr-pipeline/wasi-build/
test/pipelines/input-output-files-pipeline/wasi-build/
test/pipelines/mesh-read-write-pipeline/wasi-build/
test/pipelines/input-output-json-pipeline/emscripten-build/
test/pipelines/input-output-json-pipeline/wasi-build/

cypress/screenshots/
cypress/videos/
5 changes: 5 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -174,6 +174,11 @@
"src/bindgen/typescript-resources/*"
]
},
"standard": {
"ignore": [
"test/pipelines/input-output-json-pipeline/emscripten-build/*.js"
]
},
"release": {
"repositoryUrl": "[email protected]:InsightSoftwareConsortium/itk-wasm.git",
"branches": [
Expand Down
1 change: 1 addition & 0 deletions src/build-emscripten.js
Original file line number Diff line number Diff line change
Expand Up @@ -192,6 +192,7 @@ const testPipelines = [
path.join('test', 'pipelines', 'stdout-stderr-pipeline'),
path.join('test', 'pipelines', 'median-filter-pipeline'),
path.join('test', 'pipelines', 'input-output-files-pipeline'),
path.join('test', 'pipelines', 'input-output-json-pipeline'),
path.join('test', 'pipelines', 'mesh-read-write-pipeline'),
]

Expand Down
1 change: 1 addition & 0 deletions src/build-wasi.js
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,7 @@ const testPipelines = [
path.join('test', 'pipelines', 'stdout-stderr-pipeline'),
path.join('test', 'pipelines', 'median-filter-pipeline'),
path.join('test', 'pipelines', 'input-output-files-pipeline'),
path.join('test', 'pipelines', 'input-output-json-pipeline'),
path.join('test', 'pipelines', 'mesh-read-write-pipeline'),
]

Expand Down
16 changes: 8 additions & 8 deletions src/pipeline/internal/runPipelineEmscripten.ts
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,14 @@ function runPipelineEmscripten (pipelineModule: PipelineEmscriptenModule, args:
setPipelineModuleInputJSON(pipelineModule, dataJSON, index)
break
}
case InterfaceTypes.JsonObject:
{
const dataArray = encoder.encode(JSON.stringify(input.data))
const arrayPtr = setPipelineModuleInputArray(pipelineModule, dataArray, index, 0)
const dataJSON = { size: dataArray.buffer.byteLength, data: `data:application/vnd.itk.address,0:${arrayPtr}` }
setPipelineModuleInputJSON(pipelineModule, dataJSON, index)
break
}
case InterfaceTypes.BinaryStream:
{
const dataArray = (input.data as BinaryStream).data
Expand Down Expand Up @@ -310,10 +318,6 @@ function runPipelineEmscripten (pipelineModule: PipelineEmscriptenModule, args:
switch (output.type) {
case InterfaceTypes.TextStream:
{
// const jsonPtr = pipelineModule.ccall('itk_wasm_output_json_address', 'number', ['number', 'number'], [0, index])
// const jsonSize = pipelineModule.ccall('itk_wasm_output_json_size', 'number', ['number', 'number'], [0, index])
// const jsonArray = pipelineModule.HEAPU8.slice(jsonPtr, jsonPtr + jsonSize)
// const dataJSON = JSON.parse(decoder.decode(jsonArray))
const dataPtr = pipelineModule.ccall('itk_wasm_output_array_address', 'number', ['number', 'number', 'number'], [0, index, 0])
const dataSize = pipelineModule.ccall('itk_wasm_output_array_size', 'number', ['number', 'number', 'number'], [0, index, 0])
const dataArrayView = new Uint8Array(pipelineModule.HEAPU8.buffer, dataPtr, dataSize)
Expand All @@ -330,10 +334,6 @@ function runPipelineEmscripten (pipelineModule: PipelineEmscriptenModule, args:
}
case InterfaceTypes.BinaryStream:
{
// const jsonPtr = pipelineModule.ccall('itk_wasm_output_json_address', 'number', ['number', 'number'], [0, index])
// const jsonSize = pipelineModule.ccall('itk_wasm_output_json_size', 'number', ['number', 'number'], [0, index])
// const jsonArray = pipelineModule.HEAPU8.slice(jsonPtr, jsonPtr + jsonSize)
// const dataJSON = JSON.parse(decoder.decode(jsonArray))
const dataPtr = pipelineModule.ccall('itk_wasm_output_array_address', 'number', ['number', 'number', 'number'], [0, index, 0])
const dataSize = pipelineModule.ccall('itk_wasm_output_array_size', 'number', ['number', 'number', 'number'], [0, index, 0])
outputData = { data: memoryUint8SharedArray(pipelineModule, dataPtr, dataSize) }
Expand Down
20 changes: 20 additions & 0 deletions test/node/pipeline/runPipelineNodeTest.js
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,26 @@ test('runPipelineNode uses input and output text and binary data via memory io',
})
})

test('runPipelineNode uses input and output json data via memory io', (t) => {
const pipelinePath = path.resolve('test', 'pipelines', 'input-output-json-pipeline', 'emscripten-build', 'input-output-json-test')
const args = ['--memory-io',
'0',
'0'
]
const desiredOutputs = [
{ type: InterfaceTypes.JsonObject }
]
const jsonObject = { key1: 'text', key2: 8 }
const inputs = [
{ type: InterfaceTypes.JsonObject, data: jsonObject }
]
return runPipelineNode(pipelinePath, args, desiredOutputs, inputs)
.then(function ({ outputs }) {
t.is(outputs[0].type, InterfaceTypes.JsonObject)
t.deepEqual(outputs[0].data.data, jsonObject)
})
})

test('runPipelineNode writes and reads an itk.Image via memory io', (t) => {
const verifyImage = (image) => {
t.is(image.imageType.dimension, 2, 'dimension')
Expand Down
23 changes: 23 additions & 0 deletions test/pipelines/input-output-json-pipeline/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
cmake_minimum_required(VERSION 3.16)
project(input-output-json-test)

set(CMAKE_CXX_STANDARD 17)

find_package(ITK REQUIRED
COMPONENTS WebAssemblyInterface)
include(${ITK_USE_FILE})
add_executable(input-output-json-test input-output-json-test.cxx)
target_link_libraries(input-output-json-test PUBLIC WebAssemblyInterface)

enable_testing()
add_test(NAME InputOutputJsonObjectTest
COMMAND input-output-json-test
${CMAKE_CURRENT_SOURCE_DIR}/input-object.json
${CMAKE_CURRENT_BINARY_DIR}/output-object.json
)

add_test(NAME InputOutputJsonArrayTest
COMMAND input-output-json-test
${CMAKE_CURRENT_SOURCE_DIR}/input-array.json
${CMAKE_CURRENT_BINARY_DIR}/output-array.json
)
1 change: 1 addition & 0 deletions test/pipelines/input-output-json-pipeline/input-array.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
[8, 0, 9]
4 changes: 4 additions & 0 deletions test/pipelines/input-output-json-pipeline/input-object.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"key1": "text",
"key2": 5
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
/*=========================================================================
*
* Copyright NumFOCUS
*
* 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
*
* https://www.apache.org/licenses/LICENSE-2.0.txt
*
* 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.
*
*=========================================================================*/
#include "itkPipeline.h"
#include "itkInputTextStream.h"
#include "itkOutputTextStream.h"

#include <sstream>

int main( int argc, char * argv[] )
{
itk::wasm::Pipeline pipeline("input-output-json-test", "A pipeline to test JSON IO", argc, argv);

itk::wasm::InputTextStream inputJson;
pipeline.add_option("input-json", inputJson, "The input json")->type_name("INPUT_JSON");

itk::wasm::OutputTextStream outputJson;
pipeline.add_option("output-json", outputJson, "The output json")->type_name("OUTPUT_JSON");

ITK_WASM_PARSE(pipeline);

const size_t bufferLength = 2048;
char * buffer = new char[bufferLength];
size_t readLength = 0;

std::istream & inputTxt = inputJson.Get();
inputTxt.read( buffer, bufferLength );
readLength = inputTxt.gcount();
buffer[readLength] = '\0';

outputJson.Get().write( buffer, readLength );

delete[] buffer;

return EXIT_SUCCESS;
}

0 comments on commit e10cba1

Please sign in to comment.