tantek

 

CassisProject

Page history last edited by Tantek 1 wk ago

Introduction

 

Conceived in late 2008, the goal of the CASSIS Project is universal javascript that works on the client and the server for scalable application logic, from implementing the no javascript on the client case with the same application logic that would run on the client were the client to support javascript, especially dynamic interfaces that make use of XMLHTTPRequest (XHR/AJAX).

 

CASSIS stands for: client and server scripting implementation subset.

 

V0

 

Until typical hosting companies support running javascript on the server, CASSIS code must run in at least two programming language environments, javascript on the client, and something that can be made to resemble javascript on the server (current candidates are PHP, Ruby, and C/C++). This is the goal of CASSIS v0.

 

License

CassisProject source code (including all examples, fragments, and functions provided below) is licensed under a Creative Commons Attribution Share-Alike license (for now). Please attribute to "Tantek Çelik" and link attribution to http://tantek.com

 

Detection

Due to the need to abstract some number of language differences in syntax and built-in functionality, a foundational library must be developed that can detect which language it is running in, and execute language specific code accordingly so that CASSIS v0 code itself will not require anything language specific.

 

The key to the CASSIS v0 foundational library is the detection and switching of code dependent on which programming language environment is currently running.

 

The following code switches between javascript, PHP, C/C++ and Ruby if need be:

 

if ("00"==false) {/*javascript*/} 
else if ("0"==false) {/*php*/} 
else if (0==false) {/*c*/} 
else {/*ruby*/}

For simplicity, it is sufficient to start with the ability to switch between javascript and PHP environments:

 

function js() {
 return ("00"==false);
}

This function is called by any foundational library function that needs to execute different code for javascript vs. PHP as follows:

 

if (js()) {
 /* javascript */
}
else {
 /* PHP */
}


V0.1 code

 

 

The latest CASSIS v0.1 code is hosted live at:

 

http://tantek.com/cassis.js

 

Improvements in V0.1 over V0

  • integrated cassis.js. cassisv0php.js and cassis0.js have been fully integrated into cassis.js. This presents a much simpler inclusion model for both PHP and Javascript.
  • alternating overlapping comments instead of PHP heredoc.
  • many many more library functions.

 

 

V0 code

This section describes the initial functional version of CASSIS and is provided as historical documentation at this point (2010-031).

 

CASSIS v0 code is code that will run in PHP or javascript (and thus both on typical web servers, and typical web clients/browsers). Running CASSIS v0 code in javascript requires the use of an adapter library which re-implements some built-in PHP functions in javascript, documented in a latter section as cassisv0php.js.

 

CASSIS v0 code files must start with the following code sequence to allow both PHP and javascript to execute properly:

 

<!-- <?php // -->

 

Similarly, CASSIS v0 code files must end with the following code sequence to properly balance and close out the PHP execution environment and the SGML/HTML comment opened at the start of the file in order to reduce/minimize any unintended effects of text outputted by PHP.

 

// ?> -->

 

 

common functions

One of best ways to avoid syntax differences between PHP and javascript is to use functions as much as possible. When PHP and javascript both have native (but differing in syntax) features for particular language functionality (e.g. string concatenation), the functionality must be abstracted in a common function.  

 

The following block of CASSIS v0 code can be dropped into a CASSIS code file inline, or included as a separate cassis0.js file:

 

/* cassis0.js */
function js() {
 return ("00"==false);
}

function strcat($s1,$s2) {
 if (js()) { return $s1 + $s2; }
 else { return $s1 . $s2; }
}
/* end cassis0.js */



javascript only functions

When one language has a native feature that is implemented via a function (perhaps built-in) in the other language, it is better to use that function than the native feature, and then implement a compatibility function for the language with the native feature.

 

E.g. string length is implemented in javascript with the native "length" property, e.g. alert("foo".length) displays 3. In PHP string length is implemented as the strlen() function.  Thus we re-implement strlen() as a function in javascript using its native "length" property.

 

The following block of javascript code can be dropped into a CASSIS code file inline, or included as a separate cassis0php.js file:

 

/* cassis0php.js - processed only by javascript */
// ?>
<!-- <?php $js=<<<EJS
/* --> /**/

function strlen(s) {
 return s.length;
} 

function ord(s) {
 return s.charCodeAt(0);
}

function substr(s,o,n) {
 var m = strlen(s);
 if (Math.abs(o)>=m) return false;
 if (o<0) o=m+o;
 if (n<0) n=m-o+n;
 return s.substring(o,o+n);
}

function explode(d,s,n) {
 return s.split(d,n);
}

function rawurlencode(s) {
 return encodeURIComponent(s);
}

function htmlspecialchars(s) {
 var c= [["&","&amp;"],["<","&lt;"],[">","&gt;"],["'","&#039;"],['"',"&quot;"]];
 for (i=0;i<c.length;i++) {
  s = s.replace(c[i][0],c[i][1]);
 }
 return s;
}

function str_ireplace(a,b,s) {
 return s.replace(new RegExp(a,"gi"),b);
}

/* more javascript-only functions here */

/*
EJS;
/**/
/* end cassis0php.js */


A precise sequence of common HTML/SGML comments <!-- -->, block comments /*...*/, one line comments //, PHP execution environment entering <?php and exiting ?>, and the PHP "heredoc" <<< block string format is used to declare a function in javascript which is seen only as a string value block of text by PHP. The concept of using overlapping multi-lingual comments to pass different content to different language interpreters was first proposed and demonstrated in Semantic Scripting (posted 2002-11-21).

 

Using the above cassis0php.js code, CASSIS v0 code can call the "strlen" (to determine the length of a string) and "ord" functions, and such function calls will work in PHP or javascript.

 

PHP only functions

 

...to be written as needed.

 

Including CASSIS

 

Use the following code to include cassis.js:

 

Clientside in HTML:

<script type="text/javascript" src="cassis.js"></script>

 

Serverside in PHP:

ob_start(); // stops the few HTML comments in CASSIS from being outputted
include 'cassis.js';
ob_end_clean();


 

Implementations using Cassis

  • NewBase60 provides an open source implementation written completely in CASSIS v0.
  • H2VX uses CASSIS v0.
  • ASIN.cc uses CASSIS v0.1, directly including the same cassis.js file in script and PHP using the above documented technique.
  • Whistle is a personal URL shortener that uses CASSIS v0.1 to convert long descriptive URLs into short sharable/compressed URLs (using NewBase60) and back.
  • Falcon is a personal publishing/syndicating/tweeting/updating web application that uses CASSIS v0.1. 

 

Projects to be written

 


Return to MyNextStartup \ ProjectsList \ FrontPage.

Comments (0)

You don't have permission to comment on this page.