Can I use `TextEncoder` in Bigquery JS UDF? - javascript

I am trying to use some Rust wasm code in Bigquery as UDF, and in order to pass on Java String to Rust code the TextEncoder and TextDecoder would be needed to conveniently doing that. As it mentioned here Passing a JavaScript string to a Rust function compiled to WebAssembly
But when I try out some of my code on BigQuery, I encountered an error saying TextEncoder is not defined.
You can try it out as well with a query like this:
https://github.com/liufuyang/rb62-wasm/blob/master/try-3-old.sql
While a working version without using TextEncoder is at https://github.com/liufuyang/rb62-wasm/blob/master/try-3.sql

That means the object is not defined.
As an option, bring your own TextEncoder.
For example, take your try-3-old.sql, and then add this line at the end of the JS UDF definition:
return main();
'''
OPTIONS (library="gs://fh-bigquery/js/inexorabletash.encoding.js");
And now it works:
(wondering, what's the goal with rb62?)

Related

Can we use Buffer.from in typescript frontend code

Can we use Buffer.from to Base 64 encode a string in a front end typescript application. Using btoa() is showing as deprecated.
No you cannot. Buffer is a Node.js specific class and it does not exist in browsers.
The warning is beacause you have node types in your proyect, so Typescript Compiler thinks that you're using atob in node (which is totally deprecated) but you don't. You are totally fine using atob in front-end.
To solve the warning you can remove node types (if you're not using them), or to make it simple, a comentary before your atob call should be enought to avoid any warning from the next line:
// #ts-ignore
Your atob call goes here
Make sure you keep it simple under the #ts-ignore, you don't want to hidde a real problem there.

How to pass command line arguments to C code with WebAssembly and JS?

I'm trying to run some small C demos on the web with WebAssembly and pure JS, and I'm compiling my code using WASI-SDK/WASI-libc:
clang --target=wasm32-unknown-wasi --sysroot=<sysroot> -nostartfiles -O3 -flto -Wl,--no-entry -Wl,--export=malloc -Wl,--export-all -Wl,--lto-O3 src.c -o src.wasm
I'm then using this small JS library to implement the WASI functions. This works fine for printing with stdout, and I've even tested passing strings and other types into different functions. But I can't figure out how to pass an array of strings into main as an argument.
I don't want to use Node or Emscripten, which is why I went with a minimal JS implementation.
Edit:
To add command line arguments, I removed both -nostartfiles and -Wl,--no-entry from my compiler call and implemented args_get and args_sizes_get from the WASI standard. From there, it was as simple as calling _start from the wasm's exported functions.
If you want to use WASI then the way to pass args to main is to implement the wasi_snapshot_preview1.args_get and wasi_snapshot_preview1.args_sizes_get syscalls which are used by WASI programs to access the argv values. In that case you would want to call _start rather than main.
If you want to bypass that and call main directly you would need to somehow allocate and array of char * pointers in the linear memory which you could then pass as your argv value. The problem that you face if you try to take this approach is that allocating memory (e.g. via malloc) before calling main is hard. My advise would be to go with the first method which is to call _start and implement that wasi syscall needed to access argv. (You can call also then remove the -Wl,--no-entry from your link command).

js-yaml -> Parse ruby types

I am making a Ruby console in JavaScript and thus need to accurately send Ruby object information to the renderer process. I decided to use YAML given that it is super easy to use YAML on the Ruby side and contains all information required. But I keep getting errors.
Take the following ruby:
[Test,Test.new].to_yaml
This converts to the following YAML:
---
- !ruby/class 'Test'
- !ruby/object:Test {}
When I try to parse this with JS-YAML we get the following error:
unknown tag !<!ruby/class> at line 2, column 21:
- !ruby/class 'Test'
^
So I expect this is happenning because YAML in JavaScript doesn't have Ruby types! I've seen that one solution is to create new YAML types to handle this data:
var RubyClassType = new jsyaml.Type('!ruby/class', {
kind: 'class'
});
However, in an ideal world I wouldn't have to define each individual type. In an ideal world either, all unknown types will be treated the same (e.g. as yaml sequences) or ruby wouldn't generate the odd ruby types in the first place. Can I get around this issue without having to define every Ruby type in JavaScript?
Looks like this can be handled in JS-YAML as follows: handle_unknown_types.js
Won't accept this as an answer though as a ruby-first solution would be better.

How to get all identifier references using TypeScript API

How to call "getReferencedSymbolsForNode()" function using TypeScript Compiler API?
This function is defined here:
https://github.com/Microsoft/TypeScript/blob/master/src/services/findAllReferences.ts
but I do not understand how to call it from TS Compiler API.
I just need to get all filenames which reference to specified ts.Node.
In other words I need to retrieve the same filenames as Atom/Visual Studio Code retunes me if I press Shift+F12 (only filenames):
You want to use the TS language service to make these kind of calls. You can create one with the TypeScript compiler API which you can learn about on the TypeScript wiki: https://github.com/microsoft/TypeScript/wiki/Using-the-Compiler-API
You can see a working version in the source for typescript-vfs

How can I clone a Parse Query using the Javascript SDK on Parse Cloud Code?

I want to clone queries because it helps me structure my code better. But, when I try to clone an query, I get an error saying: Object [object Object] has no method 'skip'. Here's the code I've tried:
var _ = require('cloud/modules/lodash'); // custom lodash 4.0.0
...
var query = new Query('Class');
var clonedQuery = _.clone(query); // .cloneDeep() doesn't work either
clonedQuery.skip(i); // succeeds in unit tests but fails on cloud code
var clonedQuery.first();
Note: The underscore _ is actually lodash 4.0.0 that I've manually bundled with my source code, not the cloud module from Parse.
It's not clear to my why this is failing — it's lodash doing the cloning here, so there should be no difference between my local unit testing node environment and the V8 Cloud Code environment.
EDIT: I was using Parse SDK 1.3.1 here. Solution is to upgrade. See below.
_.clone() actually does work for Query in Parse SDK 1.6.14, but not on 1.3.1, which I was using. I didn't know that you can manage the Parse JS SDK version yourself using the parse jssdk tool!
The solution for me was simply to update the SDK. See the discussion here: https://github.com/ParsePlatform/Parse-SDK-JS/issues/171

Categories