diff --git a/Makefile b/Makefile index ba4c6d4..c21538d 100644 --- a/Makefile +++ b/Makefile @@ -282,6 +282,7 @@ WEB_BOOT = $(COMMON_PHASE1e) \ common/ansi.fs \ $(COMMON_PHASE2) \ common/tasks.fs \ + web/utils.fs \ web/fini.fs $(GEN)/web_boot.js: tools/source_to_string.js $(WEB_BOOT) | $(GEN) $< -web boot $(VERSION) $(REVISION) $(WEB_BOOT) >$@ diff --git a/web/platform.fs b/web/platform.fs index 95fcfd6..a715b7b 100644 --- a/web/platform.fs +++ b/web/platform.fs @@ -125,10 +125,10 @@ if (!globalObj.write) { } }, 50); - context.GetRawString = function(addr, len) { + context.GetRawString = function(data8, addr, len) { var data = ''; for (var i = 0; i < len; ++i) { - data += String.fromCharCode(u8[addr + i]); + data += String.fromCharCode(data8[addr + i]); } return data; }; @@ -677,19 +677,19 @@ JSWORD: smooth { f } JSWORD: setItem { value value_len key key_len session } if (session) { - sessionStorage.setItem(context.GetRawString(key, key_len), - context.GetRawString(value, value_len)); + sessionStorage.setItem(context.GetRawString(u8, key, key_len), + context.GetRawString(u8, value, value_len)); } else { - localStorage.setItem(context.GetRawString(key, key_len), - context.GetRawString(value, value_len)); + localStorage.setItem(context.GetRawString(u8, key, key_len), + context.GetRawString(u8, value, value_len)); } ~ JSWORD: getItem { dst dst_limit key key_len session -- n } if (session) { - var data = sessionStorage.getItem(context.GetRawString(key, key_len)); + var data = sessionStorage.getItem(context.GetRawString(u8, key, key_len)); } else { - var data = localStorage.getItem(context.GetRawString(key, key_len)); + var data = localStorage.getItem(context.GetRawString(u8, key, key_len)); } if (data === null) { return -1; @@ -763,6 +763,57 @@ JSWORD: ms-ticks { -- ms } return Date.now(); ~ +r~ +if (!globalObj.write) { + var filepick = document.createElement('input'); + filepick.type = 'file'; + filepick.name = 'files[]'; + filepick.style.display = 'none'; + document.body.appendChild(filepick); + filepick.onchange = function(event) { + if (event.target.files.length > 0) { + var reader = new FileReader(); + reader.onload = function(e) { + var data = context.GetRawString( + new Uint8Array(e.target.result), 0, e.target.result.byteLength); + try { + localStorage.setItem(context.filepick_filename, data); + context.filepick_result = -1; + context.filepick_filename = null; + } catch (e) { + context.filepick_result = 0; + context.filepick_filename = null; + } + }; + reader.readAsArrayBuffer(event.target.files[0]); + } else { + context.filepick_filename = null; + context.filepick_result = 0; + } + }; + context.filepick = filepick; + context.filepick_filename = null; + context.filepick_result = 0; +} +~ jseval + +JSWORD: upload-start { filename n } + context.filepick_filename = context.GetRawString(u8, filename, n); + context.filepick.click(); +~ + +JSWORD: upload-done? { -- f } + return context.filepick_filename == null ? -1 : 0; +~ + +JSWORD: upload-success? { -- f } + return context.filepick_result; +~ + +JSWORD: log { a n -- } + console.log(GetString(a, n)); +~ + forth definitions web : bye 0 terminate ; diff --git a/web/utils.fs b/web/utils.fs new file mode 100644 index 0000000..8d1ef8e --- /dev/null +++ b/web/utils.fs @@ -0,0 +1,34 @@ +\ Copyright 2022 Bradley D. Nelson +\ +\ Licensed under the Apache License, Version 2.0 (the "License"); +\ you may not use this file except in compliance with the License. +\ You may obtain a copy of the License at +\ +\ http://www.apache.org/licenses/LICENSE-2.0 +\ +\ Unless required by applicable law or agreed to in writing, software +\ distributed under the License is distributed on an "AS IS" BASIS, +\ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +\ See the License for the specific language governing permissions and +\ limitations under the License. + +web definitions + +: upload-file ( a n -- ) + upload-start + begin yield upload-done? until + upload-success? assert +; + +: upload ( "filename" ) bl parse dup assert upload-file ; + +: include-file { a n -- } + 0 0 a n 0 getItem { len } + here { buf } len allot + buf len a n 0 getItem len = assert + buf len evaluate +; + +: import s" _temp.fs" 2dup upload-file include-file ; + +forth definitions diff --git a/web/web.template.js b/web/web.template.js index 46645cf..932ee2d 100644 --- a/web/web.template.js +++ b/web/web.template.js @@ -82,10 +82,10 @@ function UPPER(ch) { function TOFLAGS(xt) { return xt - 1 * 4; } function TONAMELEN(xt) { return TOFLAGS(xt) + 1; } function TOPARAMS(xt) { return TOFLAGS(xt) + 2; } -function TOSIZE(xt) { return CELL_ALIGNED(u8[TONAMELEN(xt)>>2]) + 4 * i32[TOPARAMS(xt)>>2]; } +function TOSIZE(xt) { return CELL_ALIGNED(u8[TONAMELEN(xt)>>2]) + 4 * u16[TOPARAMS(xt)>>1]; } function TOLINK(xt) { return xt - 2 * 4; } function TONAME(xt) { - return (u8[TOFLAGS(xt)>>2] & BUILTIN_MARK) + return (u8[TOFLAGS(xt)] & BUILTIN_MARK) ? i32[TOLINK(xt)] : TOLINK(xt) - CELL_ALIGNED(u8[TONAMELEN(xt)]); } function TOBODY(xt) {