From 7aad83842cdab620e91fe73ac4731d75eb9c283a Mon Sep 17 00:00:00 2001 From: José Bollo Date: Wed, 9 Aug 2017 11:07:31 +0200 Subject: afb-api-js: first step for javascript bindings MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Adds the javascript interpreter duktape 2.3.0 for bindings written in javascript. This is a first integration. Much more work has to be done for fully providing an interesting feature. The option --jsapi allows to load a file that contains javascript. It exports its verbs using exports.verbname = function(req, args) ... where req is the opaque request object and args is the object representing arguments as returned by afb_req_json. The available functions are: - require(path) module load (ex system = require("afb:system")) - afb_req_success(req, result, info) - afb_req_fail(req, status, info) - result = afb_req_subcall(req, api, verb, args) Change-Id: I9e6f14acf3c291ce6a56a1fe88e93afbc4089106 Signed-off-by: José Bollo --- jsapi/js-hello.js | 28 ++++++++++++++++++++++++++++ 1 file changed, 28 insertions(+) create mode 100644 jsapi/js-hello.js (limited to 'jsapi') diff --git a/jsapi/js-hello.js b/jsapi/js-hello.js new file mode 100644 index 00000000..c6c41dfc --- /dev/null +++ b/jsapi/js-hello.js @@ -0,0 +1,28 @@ +var counter = 0; + +exports.ping = function(req, args) { + + afb_req_success(req, args, String(++counter)); +}; + +exports.fail = function(req, args) { + + afb_req_fail(req, "fail", String(++counter)); +}; + +exports.subcall = function(req, args) { + + if (!args.api || !args.verb) + afb_req_fail(req, "bad-args", String(++counter)); + else { + var x = afb_req_subcall_sync(req, args.api, args.verb, args.args); + if (!x) + afb_req_fail(req, "null-answer", String(++counter)); + else if (x.request.status == "success") + afb_req_success(req, x.response, String(++counter)); + else + afb_req_fail(req, x.request.status, String(++counter)); + } +}; + + -- cgit 1.2.3-korg