SlideShare a Scribd company logo
Metaprogramming
with JavaScript
Timur Shemsedinov
Research Institute of System Technologies
What is Metaprogramming?
• Templates and macroses to generate source
code in compile-time
• Self-changing programs
• Programs that generate other programs
• other meanings ?
What is Metaprogramming?
1. This is not artificial intelligence.
2. Metaprogramming is not something new,
you've always used it.
3. In programming languages ​​of Von Neumann architecture
nothing happens without metadata (architecture in which
data and instructions are stored in shared memory and
computer needs to recognize numbers, strings, addresses,
executable instruction, etc.)
What is Metaprogramming?
Programming paradigm that implies program
structure and functionality modification
programmatically
How Metaprogramming works?
Programming paradigm that implies program
structure and functionality modification
programmatically
1. When changes occur?
2. What is changing?
3. Whereby changes occur?
How Metaprogramming works?
When changes occurs?
• Design-time
• Compile-time
• Run-time
• Just-in-Time (immediate when executing/processing)
• Lazy (between executing/processing)
• On Timer
• On Pull requests
• On Push notifications
How Metaprogramming works?
What is changing?
• data types and data structures
• identifiers (class names, type names, variable names)
• calls (method/function names, dynamic binding)
• algorithm parameters
• formula syntax, regular expression syntax, etc.
• dynamic code interpretation (eval)
• data serialization/deserialization
How Metaprogramming works?
Whereby changes occurs?
• Parsing and translation of syntactic structures
• Access to objects and functions by identification name
• Full introspection
• First-class objects individuation:
• functions, using closures
• classes, using inheritance (prototypes in js)
• objects, using mixins
The problem definition
Why do we need Metaprogramming?
• To raise functionality, universality and
abstraction level of software solutions
• Dynamic domains when changing functionality
and structure is a normal mode
• Intersystem/inter-module integration,
using introspection and dynamic binding
Example 1: data definition
var names = [
"Marcus Aurelius Antoninus Augustus",
"Darth Vader",
"Victor Michailovich Glushkov",
"Gottfried Wilhelm von Leibniz",
"Mao Zedong",
"Vladimir Sergeevich Soloviov",
"Ibn Arabi",
"Lev Nikolayevich Tolstoy",
"Muammar Muhammad Abu Minyar al-Gaddafi",
"Rene Descartes",
"Fyodor Mikhailovich Dostoyevsky",
"Benedito de Espinosa"
];
Example 1: solution (without metaprogramming)
function filter(names) {
var result = [], name;
for (var i=0; i<names.length; i++) {
name = names[i];
if ( name.length >= 10 && name.length <= 200 &&
name.indexOf("Mich") > -1 &&
name.indexOf("V") === 0 &&
name.slice(-2) == "ov" &&
!( name.length >= 50 && name.length <= 65 &&
name.indexOf("Abu") > -1 &&
name.indexOf("Lev") === 0 &&
name.slice(-3) == "iov")
) result.push(name);
}
return result;
}
Example 1: extracted metadata
var conditions = {
length: [10, 200],
contains: "Mich",
starts: "V",
ends: "ov",
not: {
length: [50, 65],
contains: "Abu",
starts: "Lev",
ends: "iov"
}
};
Example 1: metamodel
function filter(names, conditions) {
var operations = {
length: function(s,v) { return s.length>=v[0] && s.length<=v[1] },
contains: function(s,v) { return s.indexOf(v) > -1 },
starts: function(s,v) { return s.indexOf(v) === 0 },
ends: function(s,v) { return s.slice(-v.length) == v },
not: function(s,v) { return !check(s,v) }
};
function check(s, conditions) {
var valid = true;
for (var key in conditions)
valid &= operations[key](s, conditions[key]);
return valid;
}
return names.filter(function(s) { return check(s, conditions); });
}
Unified model for IS module
Example 2: task definition
var tasks = [
{ interval:5000, get:"http://127.0.0.1/api/method1.json",
expect:"OK", save:"file1.json" },
{ interval:"8s", get:"http://127.0.0.1/api/method2.json",
put:"http://127.0.0.1/api/method4.json", save:"file2.json" },
{ interval:"7s", get:"http://127.0.0.1/api/method3.json",
expect:"Done", post:"http://127.0.0.1/api/method5.json" },
{ interval:"4s", load:"file1.json",
expect:"OK", put:"http://127.0.0.1/api/method6.json" },
{ interval:"9s", load:"file2.json", save:"file1.json",
post:"http://127.0.0.1/api/method7.json" },
{ interval:"3s", load:"file1.json", save:"file3.json" },
];
Example 2: metamodel
function iterate(tasks) {
function closureTask(task) { return function () {
console.dir(task);
var source;
if (task.get) source = request.get(task.get);
if (task.load) source = fs.createReadStream(task.load);
if (task.save) source.pipe(fs.createWriteStream(task.save));
if (task.post) source.pipe(request.post(task.post));
if (task.put) source.pipe(request.put(task.put));
} };
for (var i=0; i<tasks.length; i++)
setInterval(closureTask(tasks[i]), duration(tasks[i].interval));
}
Example 2: metamodel (with internal configuration)
function iterate(tasks) {
var sources = { get: request.get,
load: fs.createReadStream };
var destinations = { save: fs.createWriteStream,
post: request.post,
put: request.put };
function closureTask(task) { return function () {
console.dir(task);
var verb, source, destination;
for (key in sources)
if (task[key]) source = sources[key](task[key]);
for (key in destinations)
if (task[key]) source.pipe(destinations[key](task[key]));
} };
for (var i=0; i<tasks.length; i++)
setInterval(closureTask(tasks[i]), duration(tasks[i].interval));
}
Example 3: interpretation
// Parse duration to seconds, example: duration("1d 10h 7m 13s")
function duration(s) {
var result = 0;
if (typeof(s) == 'string') {
var days = s.match(/(d+)s*d/),
hours = s.match(/(d+)s*h/),
minutes = s.match(/(d+)s*m/),
seconds = s.match(/(d+)s*s/);
if (days) result += parseInt(days[1])*86400;
if (hours) result += parseInt(hours[1])*3600;
if (minutes) result += parseInt(minutes[1])*60;
if (seconds) result += parseInt(seconds[1]);
result = result*1000;
} if (typeof(s) == 'number') result = s;
return result;
}
Example 3: interpretation (configurable)
function duration(s) {
if (typeof(s) == 'number') return s;
var units = {
days: { rx:/(d+)s*d/, mul:86400 },
hours: { rx:/(d+)s*h/, mul:3600 },
minutes: { rx:/(d+)s*m/, mul:60 },
seconds: { rx:/(d+)s*s/, mul:1 }
};
var result = 0, unit, match;
if (typeof(s) == 'string') for (var key in units) {
unit = units[key];
match = s.match(unit.rx);
if (match) result += parseInt(match[1])*unit.mul;
}
return result*1000;
}
Example 4: introspection in metaprogramming
Example 4: introspection
var ds = wcl.AjaxDataSource({
read: { get: "examples/person/read.json" },
insert: { post: "examples/person/insert.json" },
update: { post: "examples/person/update.json" },
delete: { post: "examples/person/delete.json" },
find: { post: "examples/person/find.json" },
metadata: { post: "examples/person/metadata.json" }
});
ds.read({ id:5 }, function(err, data) {
data.phone ="+0123456789";
ds.update(data, function(err) {
console.log('Data saved');
});
});
Example 4: introspection in metaprogramming
var ds = wcl.AjaxDataSource({
introspect: { post: "examples/person/introspect.json" }
});
ds.read({ id:3 }, function(err, data) {
data.phone ="+0123456789";
ds.update(data, function(err) {
console.log('Data saved');
});
});
Example 4: introspection in metaprogramming
var ds = wcl.MemoryDataSource({ data: [
{ id:1, name:"Person 1", phone:"+380501002011",
emails:[ "person1@domain.com" ], age: 25 },
{ id:2, name:"Person 2", phone:"+380501002022",
emails:[ "person2@domain.com", "person2@domain2.com" ],
address: { city: "Kiev", street:"Khreschatit", building: "26" } },
{ id:3, name:"Person 3", phone:"+380501002033",
emails:[ "person3@domain.com" ],
tags: [ {tag:"tag1", color:"red"}, {tag:"tag2", color:"green"} ] },
]});
ds.read({ id:3 }, function(err, data) {
data.phone ="+0123456789";
ds.update(data, function(err) {
console.log('Data saved');
});
});
Metaprogramming techniques
• Task definition style: declarative, using metadata,
imperative and functional elements
• Hashes (associative arrays)
beforehand unknown key: var a = {}; a[key] = value;
• String interpretation, inventing domain-specific syntactic
structures or using universal ones (json, js, regexp ...)
• Mixins: beforehand unknown targer object/class
function mixin(a) { a.fn=function(){ ... } }
• Closures: function individuations
fn = (function(a) { return function() { return a*2 } })(value)
The findings
• Code size: generally decrease a lot but may increases code size
in rare cases (compensated by code readability)
• Speed: slightly decreases but good implementation
may remain approximately the same speed
• Flexibility: solution becomes more abstract/universal
software application scope expands
• Integration: usually much simplified integration
and requires less code changes
• Working pleasure: metaprogramming is interesting
so we have more pleasure and motivation
• Working speed: increasing development time but modification and
support tasks take less time, so total time is less
Metaprogramming
with JavaScript
Github: https://github.com/tshemsedinov/metaprogramming
Article: http://habrahabr.ru/post/227753/
Metaprogramming
with JavaScript
Thank you!
Questions please
Timur Shemsedinov
Research Institute of System Technologies

More Related Content

What's hot (20)

PDF
NodeJS
LinkMe Srl
 
PDF
Shared memory and multithreading in Node.js - Timur Shemsedinov - JSFest'19
Timur Shemsedinov
 
KEY
A million connections and beyond - Node.js at scale
Tom Croucher
 
PDF
Node.js
Jan Dillmann
 
PDF
Node.js and How JavaScript is Changing Server Programming
Tom Croucher
 
PDF
DEF CON 23 - amit ashbel and maty siman - game of hacks
Felipe Prado
 
PPT
Node js presentation
martincabrera
 
KEY
Node.js - Best practices
Felix Geisendörfer
 
PPTX
introduction to node.js
orkaplan
 
PDF
Node ppt
Tamil Selvan R S
 
PPTX
The Art of JVM Profiling
Andrei Pangin
 
PDF
Индексируем базу: как делать хорошо и не делать плохо Winter saint p 2021 m...
Андрей Новиков
 
PDF
Node.js - A Quick Tour
Felix Geisendörfer
 
PPTX
03 standard class library
eleksdev
 
KEY
Building a real life application in node js
fakedarren
 
PDF
Multithreading in Node.js and JavaScript
Timur Shemsedinov
 
PDF
MongoDB + node.js で作るソーシャルゲーム
Suguru Namura
 
PDF
MongoDB Performance Debugging
MongoDB
 
PDF
Everything you wanted to know about Stack Traces and Heap Dumps
Andrei Pangin
 
ODP
Asynchronous I/O in NodeJS - new standard or challenges?
Dinh Pham
 
NodeJS
LinkMe Srl
 
Shared memory and multithreading in Node.js - Timur Shemsedinov - JSFest'19
Timur Shemsedinov
 
A million connections and beyond - Node.js at scale
Tom Croucher
 
Node.js
Jan Dillmann
 
Node.js and How JavaScript is Changing Server Programming
Tom Croucher
 
DEF CON 23 - amit ashbel and maty siman - game of hacks
Felipe Prado
 
Node js presentation
martincabrera
 
Node.js - Best practices
Felix Geisendörfer
 
introduction to node.js
orkaplan
 
The Art of JVM Profiling
Andrei Pangin
 
Индексируем базу: как делать хорошо и не делать плохо Winter saint p 2021 m...
Андрей Новиков
 
Node.js - A Quick Tour
Felix Geisendörfer
 
03 standard class library
eleksdev
 
Building a real life application in node js
fakedarren
 
Multithreading in Node.js and JavaScript
Timur Shemsedinov
 
MongoDB + node.js で作るソーシャルゲーム
Suguru Namura
 
MongoDB Performance Debugging
MongoDB
 
Everything you wanted to know about Stack Traces and Heap Dumps
Andrei Pangin
 
Asynchronous I/O in NodeJS - new standard or challenges?
Dinh Pham
 

Similar to Metaprogramming with JavaScript (20)

PDF
"Applied Enterprise Metaprogramming in JavaScript", Vladyslav Dukhin
Fwdays
 
PPTX
Metaprogramming, Metaobject Protocols, Gradual Type Checks: Optimizing the "U...
Stefan Marr
 
PPT
Metaprogramming by brandon
MaslowB
 
PDF
TripCase Unit Testing with Jasmine
Stephen Pond
 
PPTX
Metaprogramming in ES6
Héctor Pablos López
 
PDF
Metarhia KievJS 22-Feb-2018
Timur Shemsedinov
 
PDF
Rediscovering JavaScript: The Language Behind The Libraries
Simon Willison
 
PPTX
Exploring metaprogramming using Ruby language
Harshal Hayatnagarkar
 
PDF
Refactoring
Amir Barylko
 
PDF
10 Ways To Improve Your Code( Neal Ford)
guestebde
 
PDF
A Systematic Language Engineering Approach for Prototyping Domain Specific Mo...
Luis Pedro
 
PDF
ECMAScript 6 new features
GephenSG
 
PDF
Refactoring
Amir Barylko
 
PPT
Testing Model Transformations
miso_uam
 
PPTX
What’s new in ECMAScript 6.0
Eyal Vardi
 
PPT
Rapid software evolution
borislav
 
KEY
JavaScript Growing Up
David Padbury
 
PDF
Monads and Monoids by Oleksiy Dyagilev
JavaDayUA
 
PDF
Semantic code transformations in MetaJS
Dmytro Dogadailo
 
PPTX
Awesomeness of JavaScript…almost
Quinton Sheppard
 
"Applied Enterprise Metaprogramming in JavaScript", Vladyslav Dukhin
Fwdays
 
Metaprogramming, Metaobject Protocols, Gradual Type Checks: Optimizing the "U...
Stefan Marr
 
Metaprogramming by brandon
MaslowB
 
TripCase Unit Testing with Jasmine
Stephen Pond
 
Metaprogramming in ES6
Héctor Pablos López
 
Metarhia KievJS 22-Feb-2018
Timur Shemsedinov
 
Rediscovering JavaScript: The Language Behind The Libraries
Simon Willison
 
Exploring metaprogramming using Ruby language
Harshal Hayatnagarkar
 
Refactoring
Amir Barylko
 
10 Ways To Improve Your Code( Neal Ford)
guestebde
 
A Systematic Language Engineering Approach for Prototyping Domain Specific Mo...
Luis Pedro
 
ECMAScript 6 new features
GephenSG
 
Refactoring
Amir Barylko
 
Testing Model Transformations
miso_uam
 
What’s new in ECMAScript 6.0
Eyal Vardi
 
Rapid software evolution
borislav
 
JavaScript Growing Up
David Padbury
 
Monads and Monoids by Oleksiy Dyagilev
JavaDayUA
 
Semantic code transformations in MetaJS
Dmytro Dogadailo
 
Awesomeness of JavaScript…almost
Quinton Sheppard
 
Ad

More from Timur Shemsedinov (20)

PDF
How to use Chat GPT in JavaScript optimizations for Node.js
Timur Shemsedinov
 
PDF
IT Revolution in 2023-2024: AI, GPT, business transformation, future professi...
Timur Shemsedinov
 
PDF
Node.js threads for I/O-bound tasks
Timur Shemsedinov
 
PDF
Node.js Меньше сложности, больше надежности Holy.js 2021
Timur Shemsedinov
 
PDF
Rethinking low-code
Timur Shemsedinov
 
PDF
Hat full of developers
Timur Shemsedinov
 
PDF
FwDays 2021: Metarhia Technology Stack for Node.js
Timur Shemsedinov
 
PDF
Node.js for enterprise 2021 - JavaScript Fwdays 3
Timur Shemsedinov
 
PDF
Node.js in 2021
Timur Shemsedinov
 
PDF
Node.js middleware: Never again!
Timur Shemsedinov
 
PDF
Patterns and antipatterns
Timur Shemsedinov
 
PDF
Race-conditions-web-locks-and-shared-memory
Timur Shemsedinov
 
PDF
Asynchronous programming and mutlithreading
Timur Shemsedinov
 
PDF
Node.js in 2020 - part 3
Timur Shemsedinov
 
PDF
Node.js in 2020 - part 2
Timur Shemsedinov
 
PDF
Information system structure and architecture
Timur Shemsedinov
 
PDF
Node.js in 2020 - part 1
Timur Shemsedinov
 
PDF
Web Locks API
Timur Shemsedinov
 
PDF
Node.js in 2020
Timur Shemsedinov
 
PDF
Введение в SQL
Timur Shemsedinov
 
How to use Chat GPT in JavaScript optimizations for Node.js
Timur Shemsedinov
 
IT Revolution in 2023-2024: AI, GPT, business transformation, future professi...
Timur Shemsedinov
 
Node.js threads for I/O-bound tasks
Timur Shemsedinov
 
Node.js Меньше сложности, больше надежности Holy.js 2021
Timur Shemsedinov
 
Rethinking low-code
Timur Shemsedinov
 
Hat full of developers
Timur Shemsedinov
 
FwDays 2021: Metarhia Technology Stack for Node.js
Timur Shemsedinov
 
Node.js for enterprise 2021 - JavaScript Fwdays 3
Timur Shemsedinov
 
Node.js in 2021
Timur Shemsedinov
 
Node.js middleware: Never again!
Timur Shemsedinov
 
Patterns and antipatterns
Timur Shemsedinov
 
Race-conditions-web-locks-and-shared-memory
Timur Shemsedinov
 
Asynchronous programming and mutlithreading
Timur Shemsedinov
 
Node.js in 2020 - part 3
Timur Shemsedinov
 
Node.js in 2020 - part 2
Timur Shemsedinov
 
Information system structure and architecture
Timur Shemsedinov
 
Node.js in 2020 - part 1
Timur Shemsedinov
 
Web Locks API
Timur Shemsedinov
 
Node.js in 2020
Timur Shemsedinov
 
Введение в SQL
Timur Shemsedinov
 
Ad

Recently uploaded (20)

PPTX
Agentic Automation Journey Series Day 2 – Prompt Engineering for UiPath Agents
klpathrudu
 
PDF
GetOnCRM Speeds Up Agentforce 3 Deployment for Enterprise AI Wins.pdf
GetOnCRM Solutions
 
PPTX
Equipment Management Software BIS Safety UK.pptx
BIS Safety Software
 
PPTX
Writing Better Code - Helping Developers make Decisions.pptx
Lorraine Steyn
 
PDF
Unlock Efficiency with Insurance Policy Administration Systems
Insurance Tech Services
 
PPTX
ChiSquare Procedure in IBM SPSS Statistics Version 31.pptx
Version 1 Analytics
 
PDF
Linux Certificate of Completion - LabEx Certificate
VICTOR MAESTRE RAMIREZ
 
PPTX
Change Common Properties in IBM SPSS Statistics Version 31.pptx
Version 1 Analytics
 
PDF
Digger Solo: Semantic search and maps for your local files
seanpedersen96
 
PPTX
Empowering Asian Contributions: The Rise of Regional User Groups in Open Sour...
Shane Coughlan
 
PDF
vMix Pro 28.0.0.42 Download vMix Registration key Bundle
kulindacore
 
PDF
Build It, Buy It, or Already Got It? Make Smarter Martech Decisions
bbedford2
 
PPTX
Tally_Basic_Operations_Presentation.pptx
AditiBansal54083
 
PPTX
Why Businesses Are Switching to Open Source Alternatives to Crystal Reports.pptx
Varsha Nayak
 
PDF
Online Queue Management System for Public Service Offices in Nepal [Focused i...
Rishab Acharya
 
PDF
HiHelloHR – Simplify HR Operations for Modern Workplaces
HiHelloHR
 
PDF
Why Businesses Are Switching to Open Source Alternatives to Crystal Reports.pdf
Varsha Nayak
 
PPTX
How Cloud Computing is Reinventing Financial Services
Isla Pandora
 
PDF
MiniTool Partition Wizard 12.8 Crack License Key LATEST
hashhshs786
 
PDF
Revenue streams of the Wazirx clone script.pdf
aaronjeffray
 
Agentic Automation Journey Series Day 2 – Prompt Engineering for UiPath Agents
klpathrudu
 
GetOnCRM Speeds Up Agentforce 3 Deployment for Enterprise AI Wins.pdf
GetOnCRM Solutions
 
Equipment Management Software BIS Safety UK.pptx
BIS Safety Software
 
Writing Better Code - Helping Developers make Decisions.pptx
Lorraine Steyn
 
Unlock Efficiency with Insurance Policy Administration Systems
Insurance Tech Services
 
ChiSquare Procedure in IBM SPSS Statistics Version 31.pptx
Version 1 Analytics
 
Linux Certificate of Completion - LabEx Certificate
VICTOR MAESTRE RAMIREZ
 
Change Common Properties in IBM SPSS Statistics Version 31.pptx
Version 1 Analytics
 
Digger Solo: Semantic search and maps for your local files
seanpedersen96
 
Empowering Asian Contributions: The Rise of Regional User Groups in Open Sour...
Shane Coughlan
 
vMix Pro 28.0.0.42 Download vMix Registration key Bundle
kulindacore
 
Build It, Buy It, or Already Got It? Make Smarter Martech Decisions
bbedford2
 
Tally_Basic_Operations_Presentation.pptx
AditiBansal54083
 
Why Businesses Are Switching to Open Source Alternatives to Crystal Reports.pptx
Varsha Nayak
 
Online Queue Management System for Public Service Offices in Nepal [Focused i...
Rishab Acharya
 
HiHelloHR – Simplify HR Operations for Modern Workplaces
HiHelloHR
 
Why Businesses Are Switching to Open Source Alternatives to Crystal Reports.pdf
Varsha Nayak
 
How Cloud Computing is Reinventing Financial Services
Isla Pandora
 
MiniTool Partition Wizard 12.8 Crack License Key LATEST
hashhshs786
 
Revenue streams of the Wazirx clone script.pdf
aaronjeffray
 

Metaprogramming with JavaScript

  • 2. What is Metaprogramming? • Templates and macroses to generate source code in compile-time • Self-changing programs • Programs that generate other programs • other meanings ?
  • 3. What is Metaprogramming? 1. This is not artificial intelligence. 2. Metaprogramming is not something new, you've always used it. 3. In programming languages ​​of Von Neumann architecture nothing happens without metadata (architecture in which data and instructions are stored in shared memory and computer needs to recognize numbers, strings, addresses, executable instruction, etc.)
  • 4. What is Metaprogramming? Programming paradigm that implies program structure and functionality modification programmatically
  • 5. How Metaprogramming works? Programming paradigm that implies program structure and functionality modification programmatically 1. When changes occur? 2. What is changing? 3. Whereby changes occur?
  • 6. How Metaprogramming works? When changes occurs? • Design-time • Compile-time • Run-time • Just-in-Time (immediate when executing/processing) • Lazy (between executing/processing) • On Timer • On Pull requests • On Push notifications
  • 7. How Metaprogramming works? What is changing? • data types and data structures • identifiers (class names, type names, variable names) • calls (method/function names, dynamic binding) • algorithm parameters • formula syntax, regular expression syntax, etc. • dynamic code interpretation (eval) • data serialization/deserialization
  • 8. How Metaprogramming works? Whereby changes occurs? • Parsing and translation of syntactic structures • Access to objects and functions by identification name • Full introspection • First-class objects individuation: • functions, using closures • classes, using inheritance (prototypes in js) • objects, using mixins
  • 9. The problem definition Why do we need Metaprogramming? • To raise functionality, universality and abstraction level of software solutions • Dynamic domains when changing functionality and structure is a normal mode • Intersystem/inter-module integration, using introspection and dynamic binding
  • 10. Example 1: data definition var names = [ "Marcus Aurelius Antoninus Augustus", "Darth Vader", "Victor Michailovich Glushkov", "Gottfried Wilhelm von Leibniz", "Mao Zedong", "Vladimir Sergeevich Soloviov", "Ibn Arabi", "Lev Nikolayevich Tolstoy", "Muammar Muhammad Abu Minyar al-Gaddafi", "Rene Descartes", "Fyodor Mikhailovich Dostoyevsky", "Benedito de Espinosa" ];
  • 11. Example 1: solution (without metaprogramming) function filter(names) { var result = [], name; for (var i=0; i<names.length; i++) { name = names[i]; if ( name.length >= 10 && name.length <= 200 && name.indexOf("Mich") > -1 && name.indexOf("V") === 0 && name.slice(-2) == "ov" && !( name.length >= 50 && name.length <= 65 && name.indexOf("Abu") > -1 && name.indexOf("Lev") === 0 && name.slice(-3) == "iov") ) result.push(name); } return result; }
  • 12. Example 1: extracted metadata var conditions = { length: [10, 200], contains: "Mich", starts: "V", ends: "ov", not: { length: [50, 65], contains: "Abu", starts: "Lev", ends: "iov" } };
  • 13. Example 1: metamodel function filter(names, conditions) { var operations = { length: function(s,v) { return s.length>=v[0] && s.length<=v[1] }, contains: function(s,v) { return s.indexOf(v) > -1 }, starts: function(s,v) { return s.indexOf(v) === 0 }, ends: function(s,v) { return s.slice(-v.length) == v }, not: function(s,v) { return !check(s,v) } }; function check(s, conditions) { var valid = true; for (var key in conditions) valid &= operations[key](s, conditions[key]); return valid; } return names.filter(function(s) { return check(s, conditions); }); }
  • 14. Unified model for IS module
  • 15. Example 2: task definition var tasks = [ { interval:5000, get:"http://127.0.0.1/api/method1.json", expect:"OK", save:"file1.json" }, { interval:"8s", get:"http://127.0.0.1/api/method2.json", put:"http://127.0.0.1/api/method4.json", save:"file2.json" }, { interval:"7s", get:"http://127.0.0.1/api/method3.json", expect:"Done", post:"http://127.0.0.1/api/method5.json" }, { interval:"4s", load:"file1.json", expect:"OK", put:"http://127.0.0.1/api/method6.json" }, { interval:"9s", load:"file2.json", save:"file1.json", post:"http://127.0.0.1/api/method7.json" }, { interval:"3s", load:"file1.json", save:"file3.json" }, ];
  • 16. Example 2: metamodel function iterate(tasks) { function closureTask(task) { return function () { console.dir(task); var source; if (task.get) source = request.get(task.get); if (task.load) source = fs.createReadStream(task.load); if (task.save) source.pipe(fs.createWriteStream(task.save)); if (task.post) source.pipe(request.post(task.post)); if (task.put) source.pipe(request.put(task.put)); } }; for (var i=0; i<tasks.length; i++) setInterval(closureTask(tasks[i]), duration(tasks[i].interval)); }
  • 17. Example 2: metamodel (with internal configuration) function iterate(tasks) { var sources = { get: request.get, load: fs.createReadStream }; var destinations = { save: fs.createWriteStream, post: request.post, put: request.put }; function closureTask(task) { return function () { console.dir(task); var verb, source, destination; for (key in sources) if (task[key]) source = sources[key](task[key]); for (key in destinations) if (task[key]) source.pipe(destinations[key](task[key])); } }; for (var i=0; i<tasks.length; i++) setInterval(closureTask(tasks[i]), duration(tasks[i].interval)); }
  • 18. Example 3: interpretation // Parse duration to seconds, example: duration("1d 10h 7m 13s") function duration(s) { var result = 0; if (typeof(s) == 'string') { var days = s.match(/(d+)s*d/), hours = s.match(/(d+)s*h/), minutes = s.match(/(d+)s*m/), seconds = s.match(/(d+)s*s/); if (days) result += parseInt(days[1])*86400; if (hours) result += parseInt(hours[1])*3600; if (minutes) result += parseInt(minutes[1])*60; if (seconds) result += parseInt(seconds[1]); result = result*1000; } if (typeof(s) == 'number') result = s; return result; }
  • 19. Example 3: interpretation (configurable) function duration(s) { if (typeof(s) == 'number') return s; var units = { days: { rx:/(d+)s*d/, mul:86400 }, hours: { rx:/(d+)s*h/, mul:3600 }, minutes: { rx:/(d+)s*m/, mul:60 }, seconds: { rx:/(d+)s*s/, mul:1 } }; var result = 0, unit, match; if (typeof(s) == 'string') for (var key in units) { unit = units[key]; match = s.match(unit.rx); if (match) result += parseInt(match[1])*unit.mul; } return result*1000; }
  • 20. Example 4: introspection in metaprogramming
  • 21. Example 4: introspection var ds = wcl.AjaxDataSource({ read: { get: "examples/person/read.json" }, insert: { post: "examples/person/insert.json" }, update: { post: "examples/person/update.json" }, delete: { post: "examples/person/delete.json" }, find: { post: "examples/person/find.json" }, metadata: { post: "examples/person/metadata.json" } }); ds.read({ id:5 }, function(err, data) { data.phone ="+0123456789"; ds.update(data, function(err) { console.log('Data saved'); }); });
  • 22. Example 4: introspection in metaprogramming var ds = wcl.AjaxDataSource({ introspect: { post: "examples/person/introspect.json" } }); ds.read({ id:3 }, function(err, data) { data.phone ="+0123456789"; ds.update(data, function(err) { console.log('Data saved'); }); });
  • 23. Example 4: introspection in metaprogramming var ds = wcl.MemoryDataSource({ data: [ { id:1, name:"Person 1", phone:"+380501002011", emails:[ "[email protected]" ], age: 25 }, { id:2, name:"Person 2", phone:"+380501002022", emails:[ "[email protected]", "[email protected]" ], address: { city: "Kiev", street:"Khreschatit", building: "26" } }, { id:3, name:"Person 3", phone:"+380501002033", emails:[ "[email protected]" ], tags: [ {tag:"tag1", color:"red"}, {tag:"tag2", color:"green"} ] }, ]}); ds.read({ id:3 }, function(err, data) { data.phone ="+0123456789"; ds.update(data, function(err) { console.log('Data saved'); }); });
  • 24. Metaprogramming techniques • Task definition style: declarative, using metadata, imperative and functional elements • Hashes (associative arrays) beforehand unknown key: var a = {}; a[key] = value; • String interpretation, inventing domain-specific syntactic structures or using universal ones (json, js, regexp ...) • Mixins: beforehand unknown targer object/class function mixin(a) { a.fn=function(){ ... } } • Closures: function individuations fn = (function(a) { return function() { return a*2 } })(value)
  • 25. The findings • Code size: generally decrease a lot but may increases code size in rare cases (compensated by code readability) • Speed: slightly decreases but good implementation may remain approximately the same speed • Flexibility: solution becomes more abstract/universal software application scope expands • Integration: usually much simplified integration and requires less code changes • Working pleasure: metaprogramming is interesting so we have more pleasure and motivation • Working speed: increasing development time but modification and support tasks take less time, so total time is less
  • 27. Metaprogramming with JavaScript Thank you! Questions please Timur Shemsedinov Research Institute of System Technologies