In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-17 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
This article introduces the knowledge of "how to generate bytecode from JS code". Many people will encounter this dilemma in the operation of actual cases, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!
Realization principle
The parsing of js in nodejs's kernel uses Google's V8 engine. The V8 engine has a js virtual machine built into it. With the V8 virtual machine, js code can be compiled into bytecode. The V8 virtual machine can recognize and run the bytecode directly. Therefore, the following execution logic is possible:
1. Js code-> js bytecode
2. Js bytecode-> nodejs-> run
Implementation code
(routine)
The part that generates the bytecode file:
Var v8 = require ('v8'); var fs = require (' fs'); / / read source file (JS source code) var js_code = fs.readFileSync (_ _ dirname+ "/ test.js"). ToString (); / / generate bytecode var script = new vm.Script (js_code, {produceCachedData: true}); var byte_code = script.cachedData; / / write bytecode to file fs.writeFileSync (_ _ dirname+ "/ test.jsb", byte_code)
Read and run the bytecode:
Var v8 = require ('v8'); var fs = require (' fs'); / / read the bytecode byte_code = fs.readFileSync (_ _ dirname+ "/ test.jsb") from the file; / / run var l = byte_code.slice (8,12). Reduce (function (sum, number, power) {return sum + = number * Math.pow (256, power);}); var dummyCode = ".repeat (l); script = new vm.Script (dummyCode, {cachedData: byte_code}) Script.runInThisContext ()
Running effect
Test.js is the test file for this example and the contents are as follows:
Console.log ("hello world"); console.log ("this is a test")
The running effect is as follows:
Generate bytecode, read and run bytecode. In this way, it is not complicated, and if the quantity is large, it is still a little cumbersome.
In addition, using JShaman to encrypt JS code is also suitable for nodejs code encryption, and it is also a very good means of nodejs code protection.
Matters needing attention
One thing to note when generating machine code in this way is: platform consistency, for example, if you want to run in a Linux system, you have to generate it under Linux; if you want to run on a Windows platform, you have to generate it in Windows, otherwise the generated bytecode file cannot be used properly.
This is the end of "how to generate bytecode from JS code". Thank you for reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.