refactor: bring js-sandbox project to the monorepo
This commit is contained in:
32
packages/hoppscotch-js-sandbox/src/utils.ts
Normal file
32
packages/hoppscotch-js-sandbox/src/utils.ts
Normal file
@@ -0,0 +1,32 @@
|
||||
import { Either, left, right } from "fp-ts/lib/Either";
|
||||
import * as QuickJS from "quickjs-emscripten";
|
||||
|
||||
export function marshalObjectToVM(vm: QuickJS.QuickJSVm, obj: object): Either<string, QuickJS.QuickJSHandle> {
|
||||
let jsonString
|
||||
|
||||
try {
|
||||
jsonString = JSON.stringify(obj)
|
||||
} catch (e) {
|
||||
return left("Marshaling stringification failed")
|
||||
}
|
||||
|
||||
const vmStringHandle = vm.newString(jsonString)
|
||||
|
||||
const jsonHandle = vm.getProp(vm.global, "JSON")
|
||||
const parseFuncHandle = vm.getProp(jsonHandle, "parse")
|
||||
|
||||
const parseResultHandle = vm.callFunction(parseFuncHandle, vm.undefined, vmStringHandle)
|
||||
|
||||
if (parseResultHandle.error) {
|
||||
parseResultHandle.error.dispose()
|
||||
return left("Marshaling failed")
|
||||
}
|
||||
|
||||
const resultHandle = vm.unwrapResult(parseResultHandle)
|
||||
|
||||
vmStringHandle.dispose()
|
||||
parseFuncHandle.dispose()
|
||||
jsonHandle.dispose()
|
||||
|
||||
return right(resultHandle)
|
||||
}
|
||||
Reference in New Issue
Block a user