Programming language for self-modifying code?

后端 未结 14 1749
别那么骄傲
别那么骄傲 2020-12-23 13:05
  • I am recently thinking about writing self-modifying programs, I think it may be powerful and fun. So I am currently looking for a language that allows
相关标签:
14条回答
  • 2020-12-23 13:51

    Have you looked at Java ? Java 6 has a compiler API, so you can write code and compile it within the Java VM.

    0 讨论(0)
  • 2020-12-23 13:55

    Every answer so far is about reflection/runtime compilation, but in the comments you mentioned you're interested in actual self-modifying code - code that modifies itself in-memory.

    There is no way to do this in C#, Java, or even (portably) in C - that is, you cannot modify the loaded in-memory binary using these languages.

    In general, the only way to do this is with assembly, and it's highly processor-dependent. In fact, it's highly operating-system dependent as well: to protect against polymorphic viruses, most modern operating systems (including Windows XP+, Linux, and BSD) enforce W^X, meaning you have to go through some trouble to write polymorphic executables in those operating systems, for the ones that allow it at all.

    It may be possible in some interpreted languages to have the program modify its own source-code while it's running. Perl, Python (see here), and every implementation of Javascript I know of do not allow this, though.

    0 讨论(0)
提交回复
热议问题