Security News
RubyGems.org Adds New Maintainer Role
RubyGems.org has added a new "maintainer" role that allows for publishing new versions of gems. This new permission type is aimed at improving security for gem owners and the service overall.
@webqit/subscript
Advanced tools
Subscript is a light-weight JavaScript parser and interpreter written in JavaScript; Subscript provides a completely-bendable JavaScript runtime for ambitious usecases.
You can parse()
a JavaScript expression (2 + 2) * 3
into a Subscript AST,
var subscript = Subscript.parse(expr);
then, stringify()
the Subscript AST back into its original JavaScript expression (2 + 2) * 3
,
let expr = subscript.stringify();
or even eval()
the JavaScript expression.
let result = subscript.eval();
// 12
Being an implementation of a subset of JavaScript, as the name implies, Subscript supports the everyday JavaScript that's just enough for most use-cases. That gives us something small and fast that fits anywhere.
Make any language transformation between Subscript.parse(expr)
and subscript.stringify()
/ subscript.eval()
. Subscript's syntax tree transformability offers a great way to do code transpiling, static code analysis, and more.
Subscript's eval()
feature is a standalone JavaScript runtime that supports user-defined contexts. This provides the level of runtime encapsulation that is not available with the native JavaScript's eval()
function. (Examples ahead.)
Supercharge everything with runtime traps. Subscript accepts the same trap object as a Proxy trap, for intercepting runtime assignment =
, delete
, and in
operators, and object accessors. This brings a new level of depth to JavaScript code. (Examples ahead.)
MathExpression
:
var expr1 = '7 + 8';
var exprObj1 = Subscript.parse(expr1);
// MathExpression
var result1 = exprObj1.eval();
// (Number) 15
ArrayExpression
:
var expr2 = '[ "New York City", "Lagos", "Berlin" ]';
var exprObj2 = Subscript.parse(expr2);
// ArrayExpression
var result2 = exprObj2.eval();
// (Array) [ "New York City", "Lagos", "Berlin" ]
ObjectExpression
:
var expr3 = '{ city1: "New York City", city2: "Lagos", city3: "Berlin", city4: cityNameFromContext }';
var exprObj3 = Subscript.parse(expr3);
// ObjectExpression
var result3 = exprObj3.eval();
// (Object) { city1: "New York City", city2: "Lagos", city3: "Berlin", city4: undefined }
var context = { cityNameFromContext: 'London' };
var result3 = exprObj3.eval(context);
// (Object) { city1: "New York City", city2: "Lagos", city3: "Berlin", city4: "London" }
FunctionExpression
:
var expr4 = '( arg1, arg2 ) => { return arg1 + arg2 + (argFromContext ? argFromContext : 0); }';
var exprObj4 = Subscript.parse(expr4);
// FunctionExpression
var result4 = exprObj4.eval();
// (Function) ( arg1, arg2 ) => { return arg1 + arg2 + (argFromContext ? argFromContext : 0); }
result4(10, 3);
// (Number) 13
var context = { argFromContext: 20 };
var result4 = exprObj4.eval(context);
// (Function) ( arg1, arg2 ) => { return arg1 + arg2 + (argFromContext ? argFromContext : 0); }
result4(10, 3);
// (Number) 33
.stringify()
:
var expr1 = exprObj1.stringify();
// 7 + 8
ConditionalExpression
:
var expr1 = 'age < 18 ? fname + " " + lname + " does not meet the age requirement!" : fname + " " + lname + " is old enough!"';
var exprObj1 = Subscript.parse(expr1);
// ConditionalExpression
var context = { fname: "John", lname: "Doe", age: 24 };
var result1 = exprObj1.eval();
// (String) John Doe is old enough!
var context = { fname: "John2", lname: "Doe2", age: 10 };
var result1 = exprObj1.eval(context);
// (String) John Doe does not meet the age requirement!
StringExpression
:
var expr2 = '"Today is: " + date().stringify()';
var exprObj2 = Subscript.parse(expr2);
// StringExpression
var context = { date:() => (new Date) };
var result2 = exprObj2.eval(context);
// (String) Today is: <current date>
.stringify()
:
var expr2 = exprObj2.stringify();
// "Today is: " + date().stringify()
BlockExpression
:
var expr = `
/**
* Block comments
*/
// Single line comments
delete obj1.prop1;
delete /*comment anywhere*/obj2.prop1;
return;
delete obj2.prop2;
`;
var exprObj = Subscript.parse(expr);
// BlockExpression
var context = { obj1: { prop1: "John" }, obj2: { prop1: "Doe", prop2: "Bar" } };
var result = exprObj.eval();
context;
// (Object) { obj1: { }, obj2: { prop2: "Bar" } }
IfExpression
:
var expr = `if ("prop1" in obj1) {
console.log('prop1 exists.');
} else {
console.log('prop1 does not exist. Creating it now.');
obj1.prop1 = 'John';
}
`;
var exprObj = Subscript.parse(expr);
// IfExpression
var pseudoContext = { obj1: { prop1: "John" }, obj2: { prop1: "Doe", prop2: "Bar" } };
var context = {};
var result = exprObj.eval(context, {
has: (target, property) => {
return property in pseudoContext;
},
set: (target, property, value) => {
pseudoContext[property] = value;
return true;
},
});
// (String) prop1 exists.
To report bugs or request features, please submit an issue.
MIT.
FAQs
Customizable JavaScript runtime
The npm package @webqit/subscript receives a total of 31 weekly downloads. As such, @webqit/subscript popularity was classified as not popular.
We found that @webqit/subscript demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
RubyGems.org has added a new "maintainer" role that allows for publishing new versions of gems. This new permission type is aimed at improving security for gem owners and the service overall.
Security News
Node.js will be enforcing stricter semver-major PR policies a month before major releases to enhance stability and ensure reliable release candidates.
Security News
Research
Socket's threat research team has detected five malicious npm packages targeting Roblox developers, deploying malware to steal credentials and personal data.