Rgrid - my AJAX framework with embedded virtual machine written in JavaScript

English forum for http://www.nedopc.org/nedopc

Moderator: Shaos

Post Reply
User avatar
Shaos
Admin
Posts: 23881
Joined: 09 Jan 2003 06:22
Location: Silicon Valley
Contact:

Rgrid - my AJAX framework with embedded virtual machine written in JavaScript

Post by Shaos »

I wrote most of my AJAX+VM framework Rgrid in 2012 - at that time it was called Webitable.js with a companion JS-script implementing actual VM called Webera.js (because programming language used for that VM was named Roberta, but later I renamed it to Robby) - in 2018 I combined those 2 files and since then it's a single file Rgrid.js - see GitLab for history and latest info:

https://gitlab.com/nedopc/sdk/-/tree/master/rgrid

 history

Code: Select all

Rgrid history
=============

v0.9 (2025-02-09)

 - $R object got field "license" with value "LGPL"
 - $R.prefix was renamed to $R.gif_prefix
 - Function $_ now returns null if element was not found
 - Initialization of opcache was moved to function $R.add
 - $R.iterate() now may have no arguments to start with document.body
 - $R.select() was modified to utilize that new feature of $R.iterate()

v0.8 (2023-04-30)

 - Added sourcecode URL to $R object
 - Allowed $_ to control checkbox or radio buttons as elements with value
 - Added function $append
 - Function $$_ was renamed to $$$

v0.7 (2023-04-17)

 - Added ability to send string as an argument of event
 - Fixed RECVP implementation

v0.6 (2023-04-16)

 - Fixed SEND implementation (previosly it gave JS syntax error)
 - RECV after SENDP returns 1st word of data, not size (and we ignore the rest of the packet)

v0.5 (2023-04-15)

 - Fixed implementation of COMMAND

v0.4 (2019-05-09)

 - Adaptation for updated Circuits.CC

v0.3 (2018-08-02)

 - Frameworks Webitable and Weberta were combined to single JS-file Rgrid.js

v0.2 (2012-12-24)

 - Added RobbyVM implementation named Weberta

v0.1 (2012-12-12)

 - Initial version of Webitable released under LGPL

Rgrid is used in Circuits.CC (since 2012) and http://ternaro.com (since 2018) and now I'm trying to make it suitable for creation of any kind of obfuscated websites with controls, forms etc. VM is running custom bytcode that could be seen as 16-bit retro-computer (in future I can support 32-bit and 64-bit flavors of the same VM). Basically this VM in JS is similar to WebAssembly, but it was created before WebAssembly was even a thing. Also Rgrid still works on old web-browsers (~2010) and I want to keep it that way.

Because of my laziness I never wrote a documentation for this framework, but right now I think it's time - with help of Google AI Gemini that is capable of writing good documentation using source code as an input - I will share this AI-generated documentation here (with some manual intervention where needed).
Я тут за главного - если что шлите мыло на me собака shaos точка net
User avatar
Shaos
Admin
Posts: 23881
Joined: 09 Jan 2003 06:22
Location: Silicon Valley
Contact:

Re: Rgrid - my AJAX framework with embedded virtual machine written in JavaScript

Post by Shaos »

Function $

This function is a simple wrapper around document.getElementById(). It's a shorthand for getting an HTML element by its ID. So, if you have an element in your HTML like

Code: Select all

<div id="myDiv"></div>
you can get a reference to it using

Code: Select all

$("myDiv")
This is a common practice to shorten code and make it more readable.

Function $_

This function is more complex and serves a dual purpose: getting the value of an element and setting the value of an element. It takes the element's ID as the first argument. The second argument is optional.
  • Getting the value (when 2nd argument is NOT provided):
    • It first gets the element using the $-function.
    • If the element exists:
      • If the element is a checkbox or radio button, it returns "1" if it's checked and "0" if it's not.
      • Otherwise, if the element has a value property (like input fields, select boxes, etc.), it returns the value of that property.
      • If the element doesn't have a value property (like a <div> or <span>), it returns the element's innerHTML.
    • If the element doesn't exist, it effectively returns null.
  • Setting the value (when 2nd argument is provided):
    • 2nd argument is used as internal variable to be returned (if not modified).
    • It first gets the element using the $-function.
    • If the element exists:
      • If the element is a checkbox or radio button:
        • It checks the value of 2nd argument. If it's "N", "F", or "off", it unchecks the element. If it's "Y", "T", "on", or a truthy value (using eval("("+v+")")), it checks the element. This 'eval' is potentially dangerous if 2nd argument comes from user input, as it can execute arbitrary JavaScript. It's generally better to avoid 'eval' and use more robust methods for type checking.
        • It then modifies internal variable to be "1" or "0" depending on the checkbox/radio button's new state.
      • Otherwise, if the element has a value property, it sets the value property to value of 2nd argument.
      • Otherwise (if no value property), it sets the element's innerHTML to value of 2nd argument.
      • Finally, it returns the internal variable (if it's not modified then it still has value of 2nd argument in it).
Function $$

This function seems to be a selector function. It uses $R.select (will be described later) to retrieve elements based on a selector string and cache result for later faster retreaval (cache could be claned by function $R.clear). Argument is treated as a prefix or mask with * and function returns array of elements with ID matching requested prefix/mask.

Function $$$

This function retrieves elements using function $$ (so it still expects prefix or mask as an input) and then extracts the ID of each element using the $map function (will be described later). It returns an array of the element IDs.

Example:

Code: Select all

<input type="text" id="myInput" value="Initial Value">
<div id="myDiv">Some text</div>
<input type="checkbox" id="myCheckbox">
<script type="text/javascript" src="Rgrid.js"></script>
<script>

  console.log($("myInput")); // Returns the input element
  console.log($_("myInput")); // Returns "Initial Value"
  $_("myInput", "New Value"); // Sets the input value to "New Value"
  console.log($_("myInput")); // Returns "New Value"

  console.log($_("myDiv")); // Returns "Some text"
  $_("myDiv", "New HTML"); // Sets the div's content to "New HTML"
  console.log($_("myDiv")); // Returns "New HTML"

  console.log($_("myCheckbox")); // Returns "0" (assuming it's unchecked)
  $_("myCheckbox", "on"); // Checks the checkbox
  console.log($_("myCheckbox")); // Returns "1"
  
  console.log($$("my")); // Returns array that consists of 3 element
  console.log($$$("my")); // Returns array [ "myInput", "myDiv", "myCheckbox"]
  
</script>
Line including Rgrid.js and last 2 tests were added by me, but everything else in this example was written by Gemini - pretty impressive :)

To be continued...
Я тут за главного - если что шлите мыло на me собака shaos точка net
Post Reply