CatchTheTornado / askql

AskQL is a query language that can express any data request
https://askql.org/
MIT License
387 stars 27 forks source link

feat(#580): a basic implementation of js function calling from askcript #590

Open pkarw opened 3 years ago

pkarw commented 3 years ago

With this feature developers get a new helper/factory method which can be used for passing JS modules and functions to AskScript without the need of manually warping them with resources.

Usage:

import { factory } from 'askql/askvm/factory';
const moduleToBeWrapped = {
  wrapMe1: () => {
    return 100;
  },
  wrapMe2: (arg: string) => {
    return arg;
  },
  dontWrapMe: () => {
    return 0;
  },
};

function ask(code: string) {
  return runUntyped(
    {
      resources: {  ...factory(moduleToBeWrapped, ['wrapMe1', 'wrapMe2'])},
      values,
    },
    parse(code)
  );
}

// now you can call the function `wrapMe1` of `moduleToBeWrapped` in AskScript
ask(`ask(call(get('wrapMe1')))

You can easily wrap a whole module like lodash by just simply:

  return runUntyped(
    {
      resources: {  ...factory(lodash, ['sort'])},
      values,
    },
    parse(code)
  );
czerwinskilukasz1 commented 3 years ago

@mhagmajer , do you have any specific comments on this PR?

@pkarw , I added a few minor comments if you had time, but feel free to merge it without addressing them.