Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
Download

Testing latest pari + WASM + node.js... and it works?! Wow.

28495 views
License: GPL3
ubuntu2004
1
/* Copyright (C) 2016 The PARI group.
2
3
This file is part of the PARI/GP package.
4
5
PARI/GP is free software; you can redistribute it and/or modify it under the
6
terms of the GNU General Public License as published by the Free Software
7
Foundation; either version 2 of the License, or (at your option) any later
8
version. It is distributed in the hope that it will be useful, but WITHOUT
9
ANY WARRANTY WHATSOEVER.
10
11
Check the License for details. You should have received a copy of it, along
12
with the package; see the file 'COPYING'. If not, write to the Free Software
13
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. */
14
15
#include "pari.h"
16
#include "../src/graph/rect.h"
17
#include <emscripten/emscripten.h>
18
19
void
20
pari_emscripten_wget(const char *s)
21
{
22
const char *name = stack_sprintf("/gpjs/root/%s",s);
23
emscripten_async_wget(name,s,NULL,NULL);
24
pari_err(e_MISC,"retry");
25
}
26
27
void
28
pari_emscripten_help(const char *s)
29
{
30
const char *url = "https://pari.math.u-bordeaux.fr/dochtml";
31
#if ((PARI_VERSION_CODE>>PARI_VERSION_SHIFT)&1)
32
pari_err(e_MISC,"Help: %s/help-stable/%s",url,s);
33
#else
34
pari_err(e_MISC,"Help: %s/help/%s",url,s);
35
#endif
36
}
37
38
static GEN
39
emscripten_base64(const char *s)
40
{
41
static const char *base64 =
42
"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
43
long i, j, ls = strlen(s), lt = ((ls+2)/3)*4;
44
long n = nchar2nlong(lt+1);
45
GEN g = cgetg(1+n, t_STR);
46
char *t = GSTR(g);
47
g[n] = 0L;
48
for(i=j=0; i < ls; i+=3, j+=4)
49
{
50
char s0 = s[i], s1 = i+1<ls ? s[i+1]: 0, s2 = i+2<ls ? s[i+2]: 0;
51
t[j] = base64[(s0 & 0xfc) >> 2];
52
t[j+1] = base64[((s0 & 0x3) << 4) + ((s1 & 0xf0) >> 4)];
53
t[j+2] = i+1<ls ? base64[((s1 & 0xf) << 2) + ((s2 & 0xc0) >> 6)]: '=';
54
t[j+3] = i+2<ls ? base64[s2 & 0x3f]: '=';
55
}
56
return g;
57
}
58
59
static void
60
emscripten_draw(PARI_plot *T, GEN w, GEN x, GEN y)
61
{
62
pari_sp av = avma;
63
GEN svg = emscripten_base64(rect2svg(w,x,y,NULL));
64
EM_ASM(rawPrint=true);(void)T;
65
pari_printf("<img src=\"data:image/svg+xml;charset=utf-8;base64,%Ps\">\n", svg);
66
EM_ASM(rawPrint=false);
67
set_avma(av);
68
}
69
70
static long plot_width, plot_height;
71
72
static void
73
pari_emscripten_get_plot(PARI_plot *T)
74
{
75
T->width = plot_width;
76
T->height = plot_height;
77
T->hunit = 3; //
78
T->vunit = 3; //
79
T->fwidth = 9; // font width
80
T->fheight = 12; // and height
81
gp_get_ploth_default_sizes(T);
82
T->dwidth = 0; // display width
83
T->dheight = 0; // and height
84
T->draw = &emscripten_draw;
85
}
86
87
void
88
pari_emscripten_plot_init(long width, long height)
89
{
90
plot_width = width;
91
plot_height = height;
92
pari_set_plot_engine(pari_emscripten_get_plot);
93
}
94
95