|
450 | 450 |
|
451 | 451 | const Scratch = window.Scratch = window.Scratch || {}; |
452 | 452 |
|
| 453 | +const CLOUD_PREFIX = '\u2601 '; |
| 454 | + |
453 | 455 | const runBenchmark = async function () { |
454 | 456 | const vm = new window.NotVirtualMachine(% custom-ratio % WIDTH, HEIGHT % /custom-ratio %); |
455 | 457 | Scratch.vm = vm; |
|
497 | 499 | vm.setVideoProvider(videoProvider); |
498 | 500 |
|
499 | 501 | const noop = () => null; |
500 | | - let cloudProvider; |
501 | | - |
502 | | - % cloud-localstorage % |
503 | | - cloudProvider = { |
504 | | - updateVariable(name, value) { |
505 | | - try { |
506 | | - localStorage.setItem('[s3] ' + name, value); |
507 | | - } catch (e) { |
508 | | - console.error('Cannot use localStorage?', e); |
509 | | - } |
510 | | - }, |
511 | | - createVariable: noop, |
512 | | - renameVariable: noop, |
513 | | - deleteVariable: noop, |
514 | | - requestCloseConnection: noop |
515 | | - }; |
516 | | - % /cloud-localstorage % |
517 | | - |
518 | | - % cloud-ws % |
519 | 502 | let ws; |
520 | 503 | function openConnection() { |
521 | 504 | try { |
|
528 | 511 | ws.onopen = onOpen; |
529 | 512 | ws.onclose = onClose; |
530 | 513 | } |
531 | | - openConnection(); |
532 | 514 | function onMessage(e) { |
533 | 515 | e.data.split('\n').forEach(message => { |
534 | 516 | if (message) { |
|
550 | 532 | function onClose() { |
551 | 533 | setTimeout(openConnection, 500); |
552 | 534 | } |
553 | | - cloudProvider = { |
| 535 | + const cloudProvider = { |
554 | 536 | updateVariable(name, value) { |
555 | | - sendData({method: 'set', name, value}); |
| 537 | + % special-cloud % |
| 538 | + if (name === CLOUD_PREFIX + 'eval') { |
| 539 | + try { |
| 540 | + vm.postIOData('cloud', { |
| 541 | + varUpdate: { |
| 542 | + name: CLOUD_PREFIX + 'eval output', |
| 543 | + value: eval(value) |
| 544 | + } |
| 545 | + }); |
| 546 | + } catch (err) { |
| 547 | + vm.postIOData('cloud', { |
| 548 | + varUpdate: { |
| 549 | + name: CLOUD_PREFIX + 'eval error', |
| 550 | + value: err.toString() |
| 551 | + } |
| 552 | + }); |
| 553 | + } |
| 554 | + } else % cloud-ws % if (name.startsWith(CLOUD_PREFIX + 'local storage')) % /cloud-ws % { |
| 555 | + % /special-cloud % |
| 556 | + % cloud-localstorage % |
| 557 | + try { |
| 558 | + localStorage.setItem('[s3] ' + name, value); |
| 559 | + } catch (e) { |
| 560 | + console.error('Cannot use localStorage?', e); |
| 561 | + } |
| 562 | + % /cloud-localstorage % |
| 563 | + % special-cloud % |
| 564 | + % cloud-ws % |
| 565 | + } else { |
| 566 | + % /cloud-ws % |
| 567 | + % /special-cloud % |
| 568 | + % cloud-ws % |
| 569 | + sendData({method: 'set', name, value}); |
| 570 | + % /cloud-ws % |
| 571 | + % special-cloud % |
| 572 | + } |
| 573 | + % /special-cloud % |
556 | 574 | }, |
557 | 575 | createVariable: noop, |
558 | 576 | renameVariable: noop, |
559 | 577 | deleteVariable: noop, |
| 578 | + % cloud-ws % |
560 | 579 | requestCloseConnection() { |
561 | 580 | if (ws && ws.readyState !== WebSocket.CLOSING && ws.readyState !== WebSocket.CLOSED) { |
562 | 581 | ws.onclose = noop; |
563 | 582 | ws.close(); |
564 | 583 | } |
565 | 584 | } |
| 585 | + % /cloud-ws % |
| 586 | + % cloud-localstorage-provider % |
| 587 | + requestCloseConnection: noop |
| 588 | + % /cloud-localstorage-provider % |
566 | 589 | }; |
567 | | - % /cloud-ws % |
568 | 590 |
|
569 | 591 | vm.setCompatibilityMode(COMPAT); |
570 | 592 | vm.setTurboMode(TURBO); |
|
910 | 932 | } |
911 | 933 | % /loading-image % |
912 | 934 |
|
913 | | - if (cloudProvider) vm.setCloudProvider(cloudProvider); |
| 935 | + vm.setCloudProvider(cloudProvider); |
914 | 936 | % cloud-localstorage % |
915 | 937 | const stageVariables = vm.runtime.getTargetForStage().variables; |
916 | 938 | for (const { name, isCloud } of Object.values(stageVariables)) { |
917 | 939 | if (isCloud) { |
| 940 | + % special-cloud % |
| 941 | + % cloud-ws % |
| 942 | + if (!name.startsWith(CLOUD_PREFIX + 'local storage')) continue; |
| 943 | + % /cloud-ws % |
| 944 | + % /special-cloud % |
918 | 945 | const value = localStorage.getItem('[s3] ' + name); |
919 | 946 | if (value === null) continue; |
920 | 947 | vm.postIOData('cloud', { varUpdate: { name, value } }); |
921 | 948 | } |
922 | 949 | } |
923 | 950 | window.addEventListener('storage', e => { |
924 | 951 | if (e.storageArea === localStorage && e.key.slice(0, 5) === '[s3] ') { |
| 952 | + const name = e.key.slice(5); |
| 953 | + % special-cloud % |
| 954 | + % cloud-ws % |
| 955 | + if (!name.startsWith(`[s3] ${CLOUD_PREFIX}local storage`)) return; |
| 956 | + % /cloud-ws % |
| 957 | + % /special-cloud % |
925 | 958 | vm.postIOData('cloud', { |
926 | 959 | varUpdate: { |
927 | | - name: e.key.slice(5), |
| 960 | + name, |
928 | 961 | value: e.newValue |
929 | 962 | } |
930 | 963 | }); |
931 | 964 | } |
932 | 965 | }); |
933 | 966 | % /cloud-localstorage % |
| 967 | + % cloud-ws % |
| 968 | + openConnection(); |
| 969 | + % /cloud-ws % |
934 | 970 |
|
935 | 971 | vm.greenFlag(); |
936 | 972 | }; |
|
0 commit comments