PLV8 and JS exports / referencing

From: Jon Erdman <postgresql(at)thewickedtribe(dot)net>
To: pgsql-general(at)postgresql(dot)org
Subject: PLV8 and JS exports / referencing
Date: 2014-11-05 23:01:29
Message-ID: 20141105170129.c8c830852116f5f8575cc054@thewickedtribe.net
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general


So, I was trying to use mustache.js in PG by defining a V8 function that imports it. Older versions worked fine, but in newer versions they use a class factory and I can't figure out how to reference the mustache stuff that it creates. Apparently I need to know how our V8 implementation does exports.

Here's the top of mustache.js with the class factory, and my attempted reference at the bottom (the return which gives me undefined reference). I tried various invocations and couldn't quite get it. I ended up hacking it up to remove the factory and change it to explicitly declare a variable to make it work, but I'd like to avoid that if possible.

If anyone wants to try to reproduce it, replace the snipped out bits using the contents of mustache.js from http://github.com/janl/mustache.js

CREATE OR REPLACE FUNCTION mustache(template text, view json)
RETURNS TEXT
LANGUAGE plv8
IMMUTABLE
STRICT
AS $$

(function (global, factory) {
if (typeof exports === "object" && exports) {
factory(exports); // CommonJS
} else if (typeof define === "function" && define.amd) {
define(['exports'], factory); // AMD
} else {
factory(global.Mustache = {}); // <script>
}
}(this, function (mustache) {

var Object_toString = Object.prototype.toString;
var isArray = Array.isArray || function (object) {
...
... SNIP ...
...
}));

return mustache.render(template, view);
$$;

--
Jon Erdman (aka StuckMojo)
PostgreSQL Zealot

Browse pgsql-general by date

  From Date Subject
Next Message Kirit Parmar 2014-11-06 12:22:41 Re: DATA corruption after promoting slave to master
Previous Message Tom Lane 2014-11-05 17:30:04 Re: Performance problem on 2 PG versions on same query