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

Beginner’s Guide to React Hooks: useState

Design Patterns: Using the Strategy Pattern in JavaScript

Java 8 - Stream API

What is HTML5 Canvas?

User Authentification Flow in React Native with Redux and navigation stacks

//platform.twitter.com/widgets.js from Twitter https://twitter.com/thehikeexperts

Advanced React Hooks Handbook Part-6

How to install Node js to raspberry pi (zero/other)

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

Not a Regular Password Regex

How Log4j May Infected Your Application?

The Basics of OAuth 2.0: Authorization code flow, Implicit flow, state and PKCE

Must Know Features for Java Developers in 2022