UpsilonNumworks / upsilon.js

Utility classes to interract with a Numworks calculator using WebUSB.
https://www.npmjs.com/package/numworks.js
MIT License
4 stars 2 forks source link

[bug] TypeError: record.data.arrayBuffer is not a function #4

Closed Miniontoby closed 1 year ago

Miniontoby commented 1 year ago

Describe the bug The code doesn't work anymore

To Reproduce Steps to reproduce the behavior:

  1. Clone repo + npm install
  2. edit the index.js and add to the connected function:
    var storage = await calculator.backupStorage();
    storage.records.push({"name": "test", "type": "py", "autoImport": true, "code": "print('Hello World!')\n"});
    await calculator.installStorage(storage, function() {});
  3. Run the example
  4. Go to the page and connect calculator
  5. See error in console logs

Expected behavior Just the default behavior

Screenshots image

Environment (please complete the following information):

Additional context

Uncaught (in promise) TypeError: record.data.arrayBuffer is not a function
    at Storage.__assembleStorage (Storage.js:58:50)
    at async Storage.encodeStorage (Storage.js:109:16)
    at async Numworks.installStorage (Numworks.js:548:28)
    at async eval (index.js:52:3)

Btw this didn't accure earlier. AFAIK I didn't change anything and I didn't update anything.

The only thing I did was to enable testmode because I needed it for my test, but I don't think that could have made it so... (or maybe because I installed a 3rd party app...)

Yaya-Cout commented 1 year ago

Can you log storage ?

Miniontoby commented 1 year ago

Can you log storage ?

{
    "magik": true,
    "records": [
        {
            "name": "squares",
            "type": "py",
            "data": {}
        },
        {
            "name": "parabola",
            "type": "py",
            "data": {}
        },
        {
            "name": "mandelbrot",
            "type": "py",
            "data": {}
        },
        {
            "name": "polynomial",
            "type": "py",
            "data": {}
        }
    ]
}

Oh wait this is after I already synced backup

UPDATE: Now with before syncing

UPDATE: using console.log(await calculator.backupStorage())

{
    "magik": true,
    "records": [
        {
            "name": "squares",
            "type": "py",
            "autoImport": true,
            "code": "from math import *\nfrom turtle import *\ndef squares(angle=0.5):\n  reset()\n  L=330\n  speed(10)\n  penup()\n  goto(-L/2,-L/2)\n  pendown()\n  for i in range(660):\n    forward(L)\n    left(90+angle)\n    L=L-L*sin(angle*pi/180)\n  hideturtle()"
        },
        {
            "name": "parabola",
            "type": "py",
            "autoImport": true,
            "code": "from matplotlib.pyplot import *\nfrom math import *\n\ng=9.81\n\ndef x(t,v_0,alpha):\n  return v_0*cos(alpha)*t\ndef y(t,v_0,alpha,h_0):\n  return -0.5*g*t**2+v_0*sin(alpha)*t+h_0\n\ndef vx(v_0,alpha):\n  return v_0*cos(alpha)\ndef vy(t,v_0,alpha):\n  return -g*t+v_0*sin(alpha)\n\ndef t_max(v_0,alpha,h_0):\n  return (v_0*sin(alpha)+sqrt((v_0**2)*(sin(alpha)**2)+2*g*h_0))/g\n\ndef simulation(v_0=15,alpha=pi/4,h_0=2):\n  tMax=t_max(v_0,alpha,h_0)\n  accuracy=1/10**(floor(log10(tMax))-1)\n  T_MAX=floor(tMax*accuracy)+1\n  X=[x(t/accuracy,v_0,alpha) for t in range(T_MAX)]\n  Y=[y(t/accuracy,v_0,alpha,h_0) for t in range(T_MAX)]\n  VX=[vx(v_0,alpha) for t in range(T_MAX)]\n  VY=[vy(t/accuracy,v_0,alpha) for t in range(T_MAX)]\n  for i in range(T_MAX):\n    arrow(X[i],Y[i],VX[i]/accuracy,VY[i]/accuracy)\n  grid()\n  show()"
        },
        {
            "name": "mandelbrot",
            "type": "py",
            "autoImport": true,
            "code": "# This script draws a Mandelbrot fractal set\n# N_iteration: degree of precision\nimport kandinsky\ndef mandelbrot(N_iteration):\n  for x in range(320):\n    for y in range(222):\n# Compute the mandelbrot sequence for the point c = (c_r, c_i) with start value z = (z_r, z_i)\n      z = complex(0,0)\n# Rescale to fit the drawing screen 320x222\n      c = complex(3.5*x/319-2.5, -2.5*y/221+1.25)\n      i = 0\n      while (i < N_iteration) and abs(z) < 2:\n        i = i + 1\n        z = z*z+c\n# Choose the color of the dot from the Mandelbrot sequence\n      rgb = int(255*i/N_iteration)\n      col = kandinsky.color(int(rgb),int(rgb*0.75),int(rgb*0.25))\n# Draw a pixel colored in 'col' at position (x,y)\n      kandinsky.set_pixel(x,y,col)"
        },
        {
            "name": "polynomial",
            "type": "py",
            "autoImport": true,
            "code": "from math import *\n# roots(a,b,c) computes the solutions of the equation a*x**2+b*x+c=0\ndef roots(a,b,c):\n  delta = b*b-4*a*c\n  if delta == 0:\n    return -b/(2*a)\n  elif delta > 0:\n    x_1 = (-b-sqrt(delta))/(2*a)\n    x_2 = (-b+sqrt(delta))/(2*a)\n    return x_1, x_2\n  else:\n    return None"
        }
    ]
}
Miniontoby commented 1 year ago

Btw to note, I had to do recovery to actually get the default files after I had disabled the testmode

Yaya-Cout commented 1 year ago

Does it work without the push line ?

Miniontoby commented 1 year ago

Uhmmm wait...

because I am using a little bit of a different code compared to what I actually put as example... and becuase i didnt try the exmaple, I now see the example working....

But yeah my code can be the problem then...

    readTextFile('backup.json', async (rawdata) => {
        let data = JSON.parse(rawdata);
        let storage = await calculator.backupStorage();
        data.forEach((script) => {
                        let existing = storage.records.find((record) => record.name == script.name);

                        if (!existing) storage.records.push(script);
                        else if (script.code != existing.code) data[data.indexOf(script)].code = existing.code;
                });
        await calculator.installStorage(storage, function() {});

with backup.json:

[{"name":"squares","type":"py","autoImport":true,"code":"from math import *\nfrom turtle import *\ndef squares(angle=0.5):\n  reset()\n  L=330\n  speed(10)\n  penup()\n  goto(-L/2,-L/2)\n  pendown()\n  for i in range(660):\n    forward(L)\n    left(90+angle)\n    L=L-L*sin(angle*pi/180)\n  hideturtle()"},{"name":"parabola","type":"py","autoImport":true,"code":"from matplotlib.pyplot import *\nfrom math import *\n\ng=9.81\n\ndef x(t,v_0,alpha):\n  return v_0*cos(alpha)*t\ndef y(t,v_0,alpha,h_0):\n  return -0.5*g*t**2+v_0*sin(alpha)*t+h_0\n\ndef vx(v_0,alpha):\n  return v_0*cos(alpha)\ndef vy(t,v_0,alpha):\n  return -g*t+v_0*sin(alpha)\n\ndef t_max(v_0,alpha,h_0):\n  return (v_0*sin(alpha)+sqrt((v_0**2)*(sin(alpha)**2)+2*g*h_0))/g\n\ndef simulation(v_0=15,alpha=pi/4,h_0=2):\n  tMax=t_max(v_0,alpha,h_0)\n  accuracy=1/10**(floor(log10(tMax))-1)\n  T_MAX=floor(tMax*accuracy)+1\n  X=[x(t/accuracy,v_0,alpha) for t in range(T_MAX)]\n  Y=[y(t/accuracy,v_0,alpha,h_0) for t in range(T_MAX)]\n  VX=[vx(v_0,alpha) for t in range(T_MAX)]\n  VY=[vy(t/accuracy,v_0,alpha) for t in range(T_MAX)]\n  for i in range(T_MAX):\n    arrow(X[i],Y[i],VX[i]/accuracy,VY[i]/accuracy)\n  grid()\n  show()"},{"name":"mandelbrot","type":"py","autoImport":true,"code":"# This script draws a Mandelbrot fractal set\n# N_iteration: degree of precision\nimport kandinsky\ndef mandelbrot(N_iteration):\n  for x in range(320):\n    for y in range(222):\n# Compute the mandelbrot sequence for the point c = (c_r, c_i) with start value z = (z_r, z_i)\n      z = complex(0,0)\n# Rescale to fit the drawing screen 320x222\n      c = complex(3.5*x/319-2.5, -2.5*y/221+1.25)\n      i = 0\n      while (i < N_iteration) and abs(z) < 2:\n        i = i + 1\n        z = z*z+c\n# Choose the color of the dot from the Mandelbrot sequence\n      rgb = int(255*i/N_iteration)\n      col = kandinsky.color(int(rgb),int(rgb*0.75),int(rgb*0.25))\n# Draw a pixel colored in 'col' at position (x,y)\n      kandinsky.set_pixel(x,y,col)"},{"name":"polynomial","type":"py","autoImport":true,"code":"from math import *\n# roots(a,b,c) computes the solutions of the equation a*x**2+b*x+c=0\ndef roots(a,b,c):\n  delta = b*b-4*a*c\n  if delta == 0:\n    return -b/(2*a)\n  elif delta > 0:\n    x_1 = (-b-sqrt(delta))/(2*a)\n    x_2 = (-b+sqrt(delta))/(2*a)\n    return x_1, x_2\n  else:\n    return None"},{"name":"V1","type":"lis","data":{}},{"name":"N1","type":"lis","data":{}},{"name":"X1","type":"lis","data":{}},{"name":"Y1","type":"lis","data":{}},{"name":"e1","type":"eq","data":{}},{"name":"d","type":"exp","data":{}},{"name":"lines","type":"py","autoImport":true,"code":"from math import *\nfrom kandinsky import *\nfrom time import *\nrot=0\noffset=5\nx=0\ny=0\nincrx = 1\nincry = 1\nmaxx=319\nmaxy=219\n\n#maxx=329\n#maxy=221\n\ncolorn=color(50,250,50)\nwhile 1:\n  x+=incrx\n  y+=incry\n  if x<1:\n    incrx = 1\n  if x>maxx:\n    incrx = -1\n  if y<1:\n    incry = 1\n  if y>maxy:\n    incry = -1\n  if (y==1 and x==maxx) or (y==maxy and x==maxx):\n    break\n  fill_rect(x-int(offset/2),y-int(offset/2),offset,offset,colorn)\n  sleep(0.001)"}]
Yaya-Cout commented 1 year ago

Can you push your code on a fork, so I can test it ?

Miniontoby commented 1 year ago

Oke now fixed it. Just by adding if (!script.code) return; into the .forEach