Skip to content

Commit

Permalink
Merge branch 'ayush/2075' of github.com:quadratichq/quadratic into ay…
Browse files Browse the repository at this point in the history
…ush/2075
  • Loading branch information
AyushAgrawal-A2 committed Jan 2, 2025
2 parents f382353 + 1e2bebc commit f9590ae
Show file tree
Hide file tree
Showing 6 changed files with 24 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import { getJavascriptXHROverride } from '@/app/web-workers/javascriptWebWorker/
import * as esbuild from 'esbuild-wasm';
import { LINE_NUMBER_VAR } from './javascript';
import { javascriptLibrary } from './runner/generateJavascriptForRunner';
import { COMMUNITY_A1_FILE_UPDATE_URL } from '@/shared/constants/urls';
export interface JavascriptTransformedCode {
imports: string;
code: string;
Expand Down Expand Up @@ -74,12 +75,18 @@ export function prepareJavascriptCode(
const code = withLineNumbers ? javascriptAddLineNumberVars(transform) : transform.code;
const javascriptXHROverride = getJavascriptXHROverride(proxyUrl, jwt);
const javascriptFetchOverride = getJavascriptFetchOverride(proxyUrl, jwt);
let replacedJavascriptLibrary = javascriptLibrary;
replacedJavascriptLibrary = replacedJavascriptLibrary.replace('{x:0,y:0}', `{x:${x},y:${y}}`); // replace the pos() with the correct x,y coordinates
replacedJavascriptLibrary = replacedJavascriptLibrary.replace(
'{COMMUNITY_A1_FILE_UPDATE_URL}',
COMMUNITY_A1_FILE_UPDATE_URL
); // replace the COMMUNITY_A1_FILE_UPDATE_URL with the correct url
const compiledCode =
javascriptXHROverride +
javascriptFetchOverride +
transform.imports +
(withLineNumbers ? `let ${LINE_NUMBER_VAR} = 0;` : '') +
javascriptLibrary.replace('{x:0,y:0}', `{x:${x},y:${y}}`) + // replace the pos() with the correct x,y coordinates
replacedJavascriptLibrary +
'(async() => {try{' +
'let results = await (async () => {' +
code +
Expand All @@ -90,5 +97,6 @@ export function prepareJavascriptCode(
} });` +
`} catch (e) { const error = e.message; const stack = e.stack; self.postMessage({ type: "error", error, stack, console: javascriptConsole.output() }); }` +
'})();';

return compiledCode;
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Generated file from ./compileJavascriptRunner.mjs
export const javascriptLibrary = `function convertNullToUndefined(arr){return arr.map(subArr=>subArr.map(element=>element===null?void 0:element))}function lineNumber(){try{throw new Error}catch(e){const match=e.stack.split("\\n")[3].match(/:(\\d+):(\\d+)/);if(match)return match[1]}}const createConversionError=(funcName,a1Params,oldFuncParams,sheetName)=>{const oldFunc=funcName+"("+oldFuncParams+")";let params=a1Params;sheetName&&(params=sheetName+":"+params);const newFunc="q.cells('"+params+"')";q.conversionError(oldFunc,newFunc)},getCellsConversionError=(funcName,x0,y0,x1,y1,sheetName)=>{const a1_0=q.toA1(x0,y0),a1_1=q.toA1(x1,y1);let oldFuncParams=x0+", "+y0+", "+x1;y1&&(oldFuncParams+=", "+y1),sheetName&&(oldFuncParams+=", "+sheetName),createConversionError(funcName,a1_0+":"+a1_1,oldFuncParams,sheetName)},getCellConversionError=(funcName,x,y,sheetName)=>{const a1=q.toA1(x,y);let oldFuncParams=x+", "+y;sheetName&&(oldFuncParams+=", "+sheetName),createConversionError(funcName,a1,oldFuncParams,sheetName)},getCells=(x0,y0,x1,y1,sheetName)=>{getCellsConversionError("getCells",x0,y0,x1,y1,sheetName)},cells=(x0,y0,x1,y1,sheetName)=>{getCellsConversionError("cells",x0,y0,x1,y1,sheetName)},getCellsWithHeadings=(x0,y0,x1,y1,sheetName)=>{getCellsConversionError("getCellsWithHeadings",x0,y0,x1,y1,sheetName)},getCell=(x,y,sheetName)=>{getCellConversionError("getCell",x,y,sheetName)},cell=(x,y,sheetName)=>{getCellConversionError("cell",x,y,sheetName)},c=(x,y,sheetName)=>{getCellConversionError("c",x,y,sheetName)},pos=()=>({x:0,y:0}),relCell=(deltaX,deltaY)=>{const a1=q.toA1(deltaX,deltaY,!1);let oldFuncParams=deltaX+", "+deltaY;createConversionError("relCell",a1,oldFuncParams)},relCells=(deltaX0,deltaY0,deltaX1,deltaY1)=>{const a1_0=q.toA1(deltaX0,deltaY0,!1),a1_1=q.toA1(deltaX1,deltaY1,!1),oldFuncParams=deltaX0+", "+deltaY0+", "+deltaX1+", "+deltaY1;createConversionError("relCells",a1_0+":"+a1_1,oldFuncParams)},rc=relCell;class q{static cells(a1){if(typeof a1!="string"){const line=lineNumber();throw new Error("q.cell requires at least 1 argument, received q.cell("+a1+")"+(line!==void 0?" at line "+(line-1):""))}try{let sharedBuffer=new SharedArrayBuffer(12),int32View=new Int32Array(sharedBuffer,0,3);Atomics.store(int32View,0,0),self.postMessage({type:"getCellsA1Length",sharedBuffer,a1});let result=Atomics.wait(int32View,0,0);const length=int32View[1];if(result!=="ok"||length===0)return[];const id=int32View[2];if(sharedBuffer=new SharedArrayBuffer(4+length),int32View=new Int32Array(sharedBuffer,0,1),Atomics.store(int32View,0,0),self.postMessage({type:"getCellsData",id,sharedBuffer}),result=Atomics.wait(int32View,0,0),result!=="ok")return[];let uint8View=new Uint8Array(sharedBuffer,4,length);const nonSharedBuffer=new ArrayBuffer(uint8View.byteLength),nonSharedView=new Uint8Array(nonSharedBuffer);nonSharedView.set(uint8View),sharedBuffer=void 0,int32View=void 0,uint8View=void 0;const resultsStringified=new TextDecoder().decode(nonSharedView),results=JSON.parse(resultsStringified),cells2=convertNullToUndefined(results.cells);if(cells2.forEach(row=>{row.forEach((cell2,i)=>{typeof cell2=="string"&&cell2.startsWith("___date___")&&(row[i]=new Date(parseInt(cell2.substring(10))))})}),!results.two_dimensional){if(cells2.length===1&&cells2[0].length===1)return cells2[0][0];if(cells2.every(row=>row.length===1))return cells2.map(row=>row[0]);if(cells2.length===1)return cells2[0]}return cells2}catch(e){console.warn("[javascriptLibrary] q error",e)}return[]}static toA1(x,y,absolute=!0){let column="";if(!absolute){const p=pos();x=x+p.x,y!==void 0&&(y=y+p.y)}for(;x>0;)x--,column=String.fromCharCode(x%26+65)+column,x=Math.floor(x/26);return column+y}static pos(){return pos()}static conversionError(oldFunc,newFunc){const message=oldFunc+" functionality is no longer supported. Use "+newFunc+" instead.";throw new Error(message)}}const TAB=" ";class JavascriptConsole{oldConsoleLog;logs=[];constructor(){this.oldConsoleLog=console.log,console.log=this.consoleMap,console.warn=this.consoleMap}log(...args){this.oldConsoleLog(args)}consoleMap=(...args)=>{args=args.map(a=>this.mapArgument(a)),this.logs.push(...args)};reset(){this.logs=[]}push(s){Array.isArray(s)?this.logs.push(...s):this.logs.push(s)}output(){return this.logs.length?this.logs.join(""):null}tab=n=>Array(n).fill(TAB).join("");mapArgument(a,level=0){if(Array.isArray(a)){if(a.length===0)return"Array: []\\n";let s="Array: [\\n";for(let i=0;i<a.length;i++)s+=this.tab(level+1)+i+": "+this.mapArgument(a[i],level+2);return s+this.tab(level)+"]\\n"}else{if(a===null)return"null\\n";if(typeof a=="bigint")return a.toString()+"n\\n";if(a instanceof Date)return a.toString()+"\\n";if(typeof a=="object"){let s="Object: { \\n";for(const key in a)s+=this.tab(level+1)+key+": "+this.mapArgument(a[key],level+1);return s+this.tab(level)+"}\\n"}else return typeof a=="string"?a+"\\n":a===void 0?"undefined\\n":a+"\\n"}}}const javascriptConsole=new JavascriptConsole;export{c,cell,cells,getCell,getCells,getCellsWithHeadings,javascriptConsole,pos,q,rc,relCell,relCells};
export const javascriptLibrary = `function convertNullToUndefined(arr){return arr.map(subArr=>subArr.map(element=>element===null?void 0:element))}function lineNumber(){try{throw new Error}catch(e){const match=e.stack.split("\\n")[3].match(/:(\\d+):(\\d+)/);if(match)return match[1]}}const createConversionError=(funcName,a1Params,oldFuncParams,sheetName)=>{const oldFunc=funcName+"("+oldFuncParams+")";let params=a1Params;sheetName&&(params=sheetName+":"+params);const newFunc="q.cells('"+params+"')";q.conversionError(oldFunc,newFunc)},getCellsConversionError=(funcName,x0,y0,x1,y1,sheetName)=>{const a1_0=q.toA1(x0,y0),a1_1=q.toA1(x1,y1);let oldFuncParams=x0+", "+y0+", "+x1;y1&&(oldFuncParams+=", "+y1),sheetName&&(oldFuncParams+=", "+sheetName),createConversionError(funcName,a1_0+":"+a1_1,oldFuncParams,sheetName)},getCellConversionError=(funcName,x,y,sheetName)=>{const a1=q.toA1(x,y);let oldFuncParams=x+", "+y;sheetName&&(oldFuncParams+=", "+sheetName),createConversionError(funcName,a1,oldFuncParams,sheetName)},getCells=(x0,y0,x1,y1,sheetName)=>{getCellsConversionError("getCells",x0,y0,x1,y1,sheetName)},cells=(x0,y0,x1,y1,sheetName)=>{getCellsConversionError("cells",x0,y0,x1,y1,sheetName)},getCellsWithHeadings=(x0,y0,x1,y1,sheetName)=>{getCellsConversionError("getCellsWithHeadings",x0,y0,x1,y1,sheetName)},getCell=(x,y,sheetName)=>{getCellConversionError("getCell",x,y,sheetName)},cell=(x,y,sheetName)=>{getCellConversionError("cell",x,y,sheetName)},c=(x,y,sheetName)=>{getCellConversionError("c",x,y,sheetName)},pos=()=>({x:0,y:0}),relCell=(deltaX,deltaY)=>{const a1=q.toA1(deltaX,deltaY,!1);let oldFuncParams=deltaX+", "+deltaY;createConversionError("relCell",a1,oldFuncParams)},relCells=(deltaX0,deltaY0,deltaX1,deltaY1)=>{const a1_0=q.toA1(deltaX0,deltaY0,!1),a1_1=q.toA1(deltaX1,deltaY1,!1),oldFuncParams=deltaX0+", "+deltaY0+", "+deltaX1+", "+deltaY1;createConversionError("relCells",a1_0+":"+a1_1,oldFuncParams)},rc=relCell;class q{static cells(a1){if(typeof a1!="string"){const line=lineNumber();throw new Error("q.cell requires at least 1 argument, received q.cell("+a1+")"+(line!==void 0?" at line "+(line-1):""))}try{let sharedBuffer=new SharedArrayBuffer(12),int32View=new Int32Array(sharedBuffer,0,3);Atomics.store(int32View,0,0),self.postMessage({type:"getCellsA1Length",sharedBuffer,a1});let result=Atomics.wait(int32View,0,0);const length=int32View[1];if(result!=="ok"||length===0)return[];const id=int32View[2];if(sharedBuffer=new SharedArrayBuffer(4+length),int32View=new Int32Array(sharedBuffer,0,1),Atomics.store(int32View,0,0),self.postMessage({type:"getCellsData",id,sharedBuffer}),result=Atomics.wait(int32View,0,0),result!=="ok")return[];let uint8View=new Uint8Array(sharedBuffer,4,length);const nonSharedBuffer=new ArrayBuffer(uint8View.byteLength),nonSharedView=new Uint8Array(nonSharedBuffer);nonSharedView.set(uint8View),sharedBuffer=void 0,int32View=void 0,uint8View=void 0;const resultsStringified=new TextDecoder().decode(nonSharedView),results=JSON.parse(resultsStringified),cells2=convertNullToUndefined(results.cells);if(cells2.forEach(row=>{row.forEach((cell2,i)=>{typeof cell2=="string"&&cell2.startsWith("___date___")&&(row[i]=new Date(parseInt(cell2.substring(10))))})}),!results.two_dimensional){if(cells2.length===1&&cells2[0].length===1)return cells2[0][0];if(cells2.every(row=>row.length===1))return cells2.map(row=>row[0]);if(cells2.length===1)return cells2[0]}return cells2}catch(e){console.warn("[javascriptLibrary] q error",e)}return[]}static toA1(x,y,absolute=!0){let column="";if(!absolute){const p=pos();x=x+p.x,y!==void 0&&(y=y+p.y)}for(;x>0;)x--,column=String.fromCharCode(x%26+65)+column,x=Math.floor(x/26);return column+y}static pos(){return pos()}static conversionError(oldFunc,newFunc){const message=oldFunc+" functionality is no longer supported. Use "+newFunc+" instead. Refer to the documentation at {COMMUNITY_A1_FILE_UPDATE_URL} for more details.";throw new Error(message)}}const TAB=" ";class JavascriptConsole{oldConsoleLog;logs=[];constructor(){this.oldConsoleLog=console.log,console.log=this.consoleMap,console.warn=this.consoleMap}log(...args){this.oldConsoleLog(args)}consoleMap=(...args)=>{args=args.map(a=>this.mapArgument(a)),this.logs.push(...args)};reset(){this.logs=[]}push(s){Array.isArray(s)?this.logs.push(...s):this.logs.push(s)}output(){return this.logs.length?this.logs.join(""):null}tab=n=>Array(n).fill(TAB).join("");mapArgument(a,level=0){if(Array.isArray(a)){if(a.length===0)return"Array: []\\n";let s="Array: [\\n";for(let i=0;i<a.length;i++)s+=this.tab(level+1)+i+": "+this.mapArgument(a[i],level+2);return s+this.tab(level)+"]\\n"}else{if(a===null)return"null\\n";if(typeof a=="bigint")return a.toString()+"n\\n";if(a instanceof Date)return a.toString()+"\\n";if(typeof a=="object"){let s="Object: { \\n";for(const key in a)s+=this.tab(level+1)+key+": "+this.mapArgument(a[key],level+1);return s+this.tab(level)+"}\\n"}else return typeof a=="string"?a+"\\n":a===void 0?"undefined\\n":a+"\\n"}}}const javascriptConsole=new JavascriptConsole;export{c,cell,cells,getCell,getCells,getCellsWithHeadings,javascriptConsole,pos,q,rc,relCell,relCells};
`;
export const javascriptLibraryLines = javascriptLibrary.split("\n").length;
Original file line number Diff line number Diff line change
Expand Up @@ -352,7 +352,12 @@ export class q {
* Show a conversion error message when the user tries to use an old function.
*/
static conversionError(oldFunc: string, newFunc: string): void {
const message = oldFunc + ' functionality is no longer supported. Use ' + newFunc + ' instead.';
const message =
oldFunc +
' functionality is no longer supported. Use ' +
newFunc +
' instead. Refer to the documentation at {COMMUNITY_A1_FILE_UPDATE_URL}' +
' for more details.';
throw new Error(message);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -351,7 +351,12 @@ export class q {
* Show a conversion error message when the user tries to use an old function.
*/
static conversionError(oldFunc: string, newFunc: string): void {
const message = oldFunc + ' functionality is no longer supported. Use ' + newFunc + ' instead.';
const message =
oldFunc +
' functionality is no longer supported. Use ' +
newFunc +
' instead. Refer to the documentation at {COMMUNITY_A1_FILE_UPDATE_URL}' +
' for more details.';
throw new Error(message);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,6 @@ class PythonClient {
break;

case 'clientPythonInit':
this.env = e.data.env;
return;

default:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -205,7 +205,8 @@ def to_a1(x: int, y: int, absolute: bool = True) -> str:
return f"{column}{y}"

def _conversion_error(old: str, new: str, raise_exception: bool = True):
output = f"{old} functionality is no longer supported. Use {new} instead, though this may be different if your sheet had negative values. Refer to the documentation at https://docs.quadratic.app/python-api/ for more details."
url = "https://community.quadratichq.com/t/switch-to-a1-notation-from-x-y-coordinates/"
output = f"{old} functionality is no longer supported. Use {new} instead, though this may be different if your sheet had negative values. Refer to the documentation at {url} for more details."

if raise_exception:
raise Exception(output)
Expand Down

0 comments on commit f9590ae

Please sign in to comment.