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 code
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:
<!-- <? // -->
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 */
// ?>
<!-- <? $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= [["&","&"],["<","<"],[">",">"],["'","'"],['"',"""]];
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 <? 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.
Implementations using Cassis
- NewBase60 provides an open source implementation written completely in CASSIS v0.
- H2VX uses CASSIS v0.
Projects to be written
- Parse hCalendar events and hAtom entries and generate an Atom file. Perhaps start with one or more of the open source PHP microformats parsers and distill it into Cassis.
- Personal URL shortener that converts long descriptive URLs into short sharable/compressed URLs (using NewBase60) and back.
Return to MyNextStartup \ ProjectsList \ FrontPage.
Comments (0)
You don't have permission to comment on this page.