Javet — Patch V8 Function at Source Code Level

Why is That Important?

const a = function () {
const b = 1;
return () => b;
}
const x = a();
console.log(x());
// Output is: 1

How?

def setSourceCode(sourceCode):
existingSourceCode = v8Function.getSourceCode()
(startPosition, endPosition) = v8Function.getPosition()
newSourceCode = existingSourceCode[:startPosition] + sourceCode + existingSourceCode[endPosition:]
v8Function.setSourceCode(newSourceCode)
v8Function.setPosition(startPosition, startPosition + len(sourceCode))
Memory Layout of V8 Function
originalSourceCode = v8ValueFunction.getSourceCode()
v8ValueFunction.setSourceCode(sourceCode)
v8ValueFunction.call(...)
v8ValueFunction.setSourceCode(originalSourceCode)
originalSourceCode = v8ValueFunction.getSourceCode()
if (v8ValueFunction.getJSScopeType().isClass()) {
try {
v8ValueFunction.callVoid(null);
// Now v8ValueFunction.getJSScopeType().isFunction() is true
} catch (JavetException e) {
}
}
v8ValueFunction.setSourceCode(sourceCode) // true
v8ValueFunction.call(...)
v8ValueFunction.setSourceCode(originalSourceCode)
Lifecycle of V8 Function

What is the Source Code of a Function in V8?

function abc(a, b, c) { ... } // Source code is (a, b, c) { ... }

(a, b, c) => { ... } // Source code is (a, b, c) => { ... }

--

--

--

Amateur programmer

Love podcasts or audiobooks? Learn on the go with our new app.

Recommended from Medium

The Dream Team — React with Electron

10 Javascript interview Questions and Answers

What is React?

RoyalSlider — Touch-Enabled jQuery Image Gallery [Latest Update & Pre-Licensed]

JavaScript — what is it? (v1)

How did I get acquainted with React Fiber?

How to Build Japanese GO with React, HTML, and CSS

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Sam Cao

Sam Cao

Amateur programmer

More from Medium

Multi-threading in JS, and what you can do with it!

Complete guide for relations (OneToMany, ManyToMany, OneToOne …) on NestJs (GraphQl and Rest)

__proto__ vs prototype: The Prototypal Inheritance Chain

Version Controlling | NoSql