Skip to content

Instantly share code, notes, and snippets.

@tors
Created July 30, 2013 07:02
Show Gist options
  • Select an option

  • Save tors/6110871 to your computer and use it in GitHub Desktop.

Select an option

Save tors/6110871 to your computer and use it in GitHub Desktop.
/**
* Library for Lightweight Seft-Protecting JavaScript
* By Phu H Phung 2008
* Version: 1.1a
* Last modified: 2008, Sept 16
*/
(function() {
var _debug = true;
/**
* The two following function is modified from
* jQuery AOP - jQuery plugin to add features of aspect-oriented programming (AOP) to jQuery.
* http://jquery-aop.googlecode.com/
*
* Licensed under the MIT license:
* http://www.opensource.org/licenses/mit-license.php
*
*/
var wrap = function(pointcut, Policy) {
var source = (typeof(pointcut.target.prototype) != 'undefined') ? pointcut.target.prototype : pointcut.target;
var method = pointcut.method
var original = source[method];
var aspect = function() {
var invocation = { object: this, args: arguments };
return Policy.apply(invocation.object, [{ arguments: invocation.args, method: method, proceed :
function() {
return original.apply(invocation.object, invocation.args);
}
}] );
};
source[method] = aspect;
return aspect;
};
var enforcePolicy = function(objectMethod, Policy){
return wrap(objectMethod, Policy);
}
var debug = function(s){
if (_debug){
document.writeln(s);
}
};
//This anonymous function contains PropertyMonitor
//and security policy
(function(){
var SecurityStates = {
statestable: new Array(),
getState: function (state) {
return this.statestable[state];
},
setState: function (state, val) {
this.statestable[state] = val;
},
init: function () {
if (this.statestable == null) {
this.statestable = new Array ();
}
this.statestable.push (new Object());
return (this.statestable.length - 1);
}
};
var PropertiesCache = {
cachetable: new Array(),
getProperty: function (property) {
return this.cachetable[property];
},
setProperty: function (property, val) {
this.cachetable[property] = val;
},
init: function () {
if (this.cachetable == null) {
this.cachetable = new Array ();
}
this.cachetable.push (new Object());
return (this.cachetable.length - 1);
}
};
PropertiesCache.init();
var MonitorPropertyGet = function (source, property, gettingAdvice) {
var id = source + "." + property;
var val = PropertiesCache.getProperty(id);
if ( (val == 'undefined') || (val == null)){
PropertiesCache.setProperty(id,eval(id));
}
var cmd = source +".__proto__.__defineGetter__('"+property+"',function(){"+
"gettingAdvice.apply(this,arguments);" +
"return PropertiesCache.getProperty(id);})";
eval(cmd);
};
var MonitorPropertySet = function (source, property, settingAdvice) {
var id = source + "." + property;
var cmd = source +".__proto__.__defineSetter__('"+property+"',function(val){"+
"settingAdvice.apply(this,arguments);" +
"PropertiesCache.setProperty(id,val);})";
eval(cmd);
};
var MonitorProperty = function (source, property, gettingAdvice, settingAdvice) {
MonitorPropertyGet(source, property, gettingAdvice);
MonitorPropertySet(source, property, settingAdvice);
};
SecurityStates.init();
})();
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment