Update precompile.md

This commit is contained in:
blueloveTH 2024-04-14 01:51:14 +08:00
parent 57574bd8be
commit 2b74fc847e
2 changed files with 8 additions and 17 deletions

View File

@ -3,7 +3,7 @@ icon: dot
title: Precompiling title: Precompiling
--- ---
pkpy allows you to precompile your python code into a special form, which can be executed later. pkpy allows you to precompile python code into two special forms, which can be executed later.
### In-memory precompilation ### In-memory precompilation
@ -15,8 +15,9 @@ CodeObject_ code = vm->compile("print('Hello, world!')", "<string>", EXEC_MODE);
vm->_exec(code); // Hello, world! vm->_exec(code); // Hello, world!
``` ```
This `CodeObject_` object is a very non-generic form of the compiled code. This `CodeObject_` object is a very non-generic form of the compiled code,
It is an in-memory form. You are not able to save it to a file or load it from a file. which is an in-memory form. Very efficient, but not portable.
You are not able to save it to a file or load it from a file.
### String precompilation ### String precompilation
@ -112,3 +113,7 @@ Traceback (most recent call last):
return g(a, b) return g(a, b)
StackOverflowError StackOverflowError
``` ```
!!!
String compilation has no guarantee of compatibility between different versions of pkpy.
!!!

View File

@ -1,16 +1,2 @@
code = compile("1+2", "<eval>", "eval") code = compile("1+2", "<eval>", "eval")
assert eval(code) == 3 assert eval(code) == 3
src = """
def f(a, b):
return g(a, b)
def g(a, b):
c = f(a, b)
d = g(a, b)
return c + d
"""
code = compile(src, "<12>", "exec")
exec(code)
f(1, 2)