Testing latest pari + WASM + node.js... and it works?! Wow.
License: GPL3
ubuntu2004
/* Copyright (C) 2016 The PARI group.12This file is part of the PARI/GP package.34PARI/GP is free software; you can redistribute it and/or modify it under the5terms of the GNU General Public License as published by the Free Software6Foundation; either version 2 of the License, or (at your option) any later7version. It is distributed in the hope that it will be useful, but WITHOUT8ANY WARRANTY WHATSOEVER.910Check the License for details. You should have received a copy of it, along11with the package; see the file 'COPYING'. If not, write to the Free Software12Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. */1314#include "pari.h"15#include "../src/graph/rect.h"16#include <emscripten/emscripten.h>1718void19pari_emscripten_wget(const char *s)20{21const char *name = stack_sprintf("/gpjs/root/%s",s);22emscripten_async_wget(name,s,NULL,NULL);23pari_err(e_MISC,"retry");24}2526void27pari_emscripten_help(const char *s)28{29const char *url = "https://pari.math.u-bordeaux.fr/dochtml";30#if ((PARI_VERSION_CODE>>PARI_VERSION_SHIFT)&1)31pari_err(e_MISC,"Help: %s/help-stable/%s",url,s);32#else33pari_err(e_MISC,"Help: %s/help/%s",url,s);34#endif35}3637static GEN38emscripten_base64(const char *s)39{40static const char *base64 =41"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";42long i, j, ls = strlen(s), lt = ((ls+2)/3)*4;43long n = nchar2nlong(lt+1);44GEN g = cgetg(1+n, t_STR);45char *t = GSTR(g);46g[n] = 0L;47for(i=j=0; i < ls; i+=3, j+=4)48{49char s0 = s[i], s1 = i+1<ls ? s[i+1]: 0, s2 = i+2<ls ? s[i+2]: 0;50t[j] = base64[(s0 & 0xfc) >> 2];51t[j+1] = base64[((s0 & 0x3) << 4) + ((s1 & 0xf0) >> 4)];52t[j+2] = i+1<ls ? base64[((s1 & 0xf) << 2) + ((s2 & 0xc0) >> 6)]: '=';53t[j+3] = i+2<ls ? base64[s2 & 0x3f]: '=';54}55return g;56}5758static void59emscripten_draw(PARI_plot *T, GEN w, GEN x, GEN y)60{61pari_sp av = avma;62GEN svg = emscripten_base64(rect2svg(w,x,y,NULL));63EM_ASM(rawPrint=true);(void)T;64pari_printf("<img src=\"data:image/svg+xml;charset=utf-8;base64,%Ps\">\n", svg);65EM_ASM(rawPrint=false);66set_avma(av);67}6869static long plot_width, plot_height;7071static void72pari_emscripten_get_plot(PARI_plot *T)73{74T->width = plot_width;75T->height = plot_height;76T->hunit = 3; //77T->vunit = 3; //78T->fwidth = 9; // font width79T->fheight = 12; // and height80gp_get_ploth_default_sizes(T);81T->dwidth = 0; // display width82T->dheight = 0; // and height83T->draw = &emscripten_draw;84}8586void87pari_emscripten_plot_init(long width, long height)88{89plot_width = width;90plot_height = height;91pari_set_plot_engine(pari_emscripten_get_plot);92}939495