├── .gitignore
├── .travis.yml
├── LICENSE
├── README.md
├── app
├── .gitignore
├── build.gradle
├── proguard-rules.pro
└── src
│ ├── androidTest
│ └── java
│ │ └── com
│ │ └── jmeng
│ │ └── luadroid
│ │ └── ExampleInstrumentedTest.java
│ ├── main
│ ├── AndroidManifest.xml
│ ├── java
│ │ └── com
│ │ │ └── jmengxy
│ │ │ └── luadroid
│ │ │ └── MainActivity.java
│ └── res
│ │ ├── layout
│ │ └── activity_main.xml
│ │ ├── mipmap-hdpi
│ │ ├── ic_launcher.png
│ │ └── ic_launcher_round.png
│ │ ├── mipmap-mdpi
│ │ ├── ic_launcher.png
│ │ └── ic_launcher_round.png
│ │ ├── mipmap-xhdpi
│ │ ├── ic_launcher.png
│ │ └── ic_launcher_round.png
│ │ ├── mipmap-xxhdpi
│ │ ├── ic_launcher.png
│ │ └── ic_launcher_round.png
│ │ ├── mipmap-xxxhdpi
│ │ ├── ic_launcher.png
│ │ └── ic_launcher_round.png
│ │ └── values
│ │ ├── colors.xml
│ │ ├── strings.xml
│ │ └── styles.xml
│ └── test
│ └── java
│ └── com
│ └── jmeng
│ └── luadroid
│ └── LuaDroidTest.java
├── build.gradle
├── gradle.properties
├── gradle
└── wrapper
│ ├── gradle-wrapper.jar
│ └── gradle-wrapper.properties
├── gradlew
├── gradlew.bat
├── lualib
├── .gitignore
├── build.gradle
└── src
│ └── main
│ ├── AndroidManifest.xml
│ ├── cpp
│ ├── CMakeLists.txt
│ ├── lua
│ │ ├── lapi.c
│ │ ├── lapi.h
│ │ ├── lauxlib.c
│ │ ├── lauxlib.h
│ │ ├── lbaselib.c
│ │ ├── lbitlib.c
│ │ ├── lcode.c
│ │ ├── lcode.h
│ │ ├── lcorolib.c
│ │ ├── lctype.c
│ │ ├── lctype.h
│ │ ├── ldblib.c
│ │ ├── ldebug.c
│ │ ├── ldebug.h
│ │ ├── ldo.c
│ │ ├── ldo.h
│ │ ├── ldump.c
│ │ ├── lfunc.c
│ │ ├── lfunc.h
│ │ ├── lgc.c
│ │ ├── lgc.h
│ │ ├── linit.c
│ │ ├── liolib.c
│ │ ├── llex.c
│ │ ├── llex.h
│ │ ├── llimits.h
│ │ ├── lmathlib.c
│ │ ├── lmem.c
│ │ ├── lmem.h
│ │ ├── loadlib.c
│ │ ├── lobject.c
│ │ ├── lobject.h
│ │ ├── lopcodes.c
│ │ ├── lopcodes.h
│ │ ├── loslib.c
│ │ ├── lparser.c
│ │ ├── lparser.h
│ │ ├── lprefix.h
│ │ ├── lstate.c
│ │ ├── lstate.h
│ │ ├── lstring.c
│ │ ├── lstring.h
│ │ ├── lstrlib.c
│ │ ├── ltable.c
│ │ ├── ltable.h
│ │ ├── ltablib.c
│ │ ├── ltm.c
│ │ ├── ltm.h
│ │ ├── lua.h
│ │ ├── lua.hpp
│ │ ├── luaconf.h
│ │ ├── lualib.h
│ │ ├── lundump.c
│ │ ├── lundump.h
│ │ ├── lutf8lib.c
│ │ ├── lvm.c
│ │ ├── lvm.h
│ │ ├── lzio.c
│ │ └── lzio.h
│ └── luax.cpp
│ ├── java
│ └── com
│ │ └── jmengxy
│ │ └── lualib
│ │ └── Lua.java
│ └── res
│ └── values
│ └── values.xml
└── settings.gradle
/.gitignore:
--------------------------------------------------------------------------------
1 | # built application files
2 | *.apk
3 | *.ap_
4 |
5 | # files for the dex VM
6 | *.dex
7 |
8 | # Java class files
9 | *.class
10 |
11 | # generated files
12 | bin/
13 | gen/
14 | .gradle/
15 | build/
16 | lualib/.externalNativeBuild/
17 | app/.externalNativeBuild/
18 |
19 | # Local configuration file (sdk path, etc)
20 | local.properties
21 |
22 | # Eclipse project files
23 | .classpath
24 | .project
25 |
26 | # Proguard folder generated by Eclipse
27 | proguard/
28 |
29 | # Intellij project files
30 | *.iml
31 | *.ipr
32 | *.iws
33 | .idea/
34 |
35 |
--------------------------------------------------------------------------------
/.travis.yml:
--------------------------------------------------------------------------------
1 | language: android
2 | sudo: true
3 | android:
4 | components:
5 | # Uncomment the lines below if you want to
6 | # use the latest revision of Android SDK Tools
7 | - tools
8 | - platform-tools
9 |
10 | # The BuildTools version used by your project
11 | - build-tools-26.0.2
12 |
13 | # The SDK version used to compile your project
14 | - android-26
15 | - extra-google-m2repository
16 | - extra-android-m2repository
17 | addons:
18 | apt_packages:
19 | - pandoc
20 | artifacts:
21 | paths:
22 | - $(git ls-files -o | grep app/build/outputs | tr "\n" ":")
23 |
24 | before_install:
25 | - rm -fr $HOME/android-ndk-r14b
26 | - curl -L http://dl.google.com/android/repository/android-ndk-r14b-linux-x86_64.zip -O
27 | - unzip -oq android-ndk-r14b-linux-x86_64.zip
28 | - rm android-ndk-r14b-linux-x86_64.zip
29 | - export ANDROID_NDK_HOME=$HOME/android-ndk-r14b
30 | - export PATH=${ANDROID_NDK_HOME}:${PATH}
--------------------------------------------------------------------------------
/LICENSE:
--------------------------------------------------------------------------------
1 | MIT License
2 |
3 | Copyright (c) 2017 Jie Meng
4 |
5 | Permission is hereby granted, free of charge, to any person obtaining a copy
6 | of this software and associated documentation files (the "Software"), to deal
7 | in the Software without restriction, including without limitation the rights
8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9 | copies of the Software, and to permit persons to whom the Software is
10 | furnished to do so, subject to the following conditions:
11 |
12 | The above copyright notice and this permission notice shall be included in all
13 | copies or substantial portions of the Software.
14 |
15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21 | SOFTWARE.
22 |
--------------------------------------------------------------------------------
/README.md:
--------------------------------------------------------------------------------
1 | # LuaDroid -- Lua interpreter for Android
2 |
3 | ## How to import LuaDroid into Android project
4 |
5 | 1. Add the JitPack repository to your build file
6 | - gradle
7 |
8 | Add it in your root build.gradle at the end of repositories:
9 |
10 | ```
11 | allprojects {
12 | repositories {
13 | ...
14 | maven { url 'https://jitpack.io' }
15 | }
16 | }
17 | ```
18 |
19 | - maven
20 |
21 | ```
22 |
23 |
24 | jitpack.io
25 | https://jitpack.io
26 |
27 |
28 | ```
29 |
30 | 2. Add the dependency
31 | - gradle
32 |
33 | ```
34 | dependencies {
35 | compile 'com.github.jie-meng:LuaDroid:V1.0.0'
36 | }
37 | ```
38 |
39 | - maven
40 |
41 | ```
42 |
43 | com.github.jie-meng
44 | LuaDroid
45 | V1.0.0
46 |
47 | ```
48 |
49 | ## Quick start
50 |
51 | ```
52 | //create instance
53 | lua = new Lua();
54 |
55 | //get
56 | String s = lua.getString("s", "");
57 | int i = lua.getInteger("i", 0);
58 | double d = lua.getDouble("d", 0.0);
59 | boolean b = lua.getBoolean("b", false);
60 |
61 | //set
62 | lua.setString("s", "test_string");
63 | lua.setInteger("i", 21);
64 | lua.setDouble("d", 3.14);
65 | lua.setBoolean("b", true);
66 |
67 | //others
68 | int type = lua.getType("s");
69 | boolean b = lua.isInteger("i");
70 | Pair result = lua.parseLine("s = string.lower('Text')");
71 | Pair result = lua.parseFile("/home/user/test.lua");
72 |
73 | //free resource
74 | lua.close();
75 | ```
76 |
--------------------------------------------------------------------------------
/app/.gitignore:
--------------------------------------------------------------------------------
1 | /build
2 |
--------------------------------------------------------------------------------
/app/build.gradle:
--------------------------------------------------------------------------------
1 | apply plugin: 'com.android.application'
2 |
3 | android {
4 | compileSdkVersion 26
5 | defaultConfig {
6 | applicationId "com.jmengxy.luadroid"
7 | minSdkVersion 15
8 | targetSdkVersion 26
9 | versionCode 1
10 | versionName "1.0"
11 | testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
12 | }
13 | buildToolsVersion "26.0.2"
14 |
15 | buildTypes {
16 | release {
17 | minifyEnabled false
18 | proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
19 | }
20 | }
21 |
22 | compileOptions {
23 | sourceCompatibility JavaVersion.VERSION_1_8
24 | targetCompatibility JavaVersion.VERSION_1_8
25 | }
26 | }
27 |
28 | dependencies {
29 | implementation fileTree(include: ['*.jar'], dir: 'libs')
30 | implementation project(':lualib')
31 | androidTestImplementation('com.android.support.test.espresso:espresso-core:2.2.2', {
32 | exclude group: 'com.android.support', module: 'support-annotations'
33 | })
34 | implementation "com.android.support:appcompat-v7:$support_lib_version"
35 | implementation 'com.android.support.constraint:constraint-layout:1.0.1'
36 | testImplementation 'junit:junit:4.12'
37 | }
38 |
--------------------------------------------------------------------------------
/app/proguard-rules.pro:
--------------------------------------------------------------------------------
1 | # Add project specific ProGuard rules here.
2 | # By default, the flags in this file are appended to flags specified
3 | # in /Users/jiemeng/Library/Android/sdk/tools/proguard/proguard-android.txt
4 | # You can edit the include path and order by changing the proguardFiles
5 | # directive in build.gradle.
6 | #
7 | # For more details, see
8 | # http://developer.android.com/guide/developing/tools/proguard.html
9 |
10 | # Add any project specific keep options here:
11 |
12 | # If your project uses WebView with JS, uncomment the following
13 | # and specify the fully qualified class name to the JavaScript interface
14 | # class:
15 | #-keepclassmembers class fqcn.of.javascript.interface.for.webview {
16 | # public *;
17 | #}
18 |
19 | # Uncomment this to preserve the line number information for
20 | # debugging stack traces.
21 | #-keepattributes SourceFile,LineNumberTable
22 |
23 | # If you keep the line number information, uncomment this to
24 | # hide the original source file name.
25 | #-renamesourcefileattribute SourceFile
26 |
--------------------------------------------------------------------------------
/app/src/androidTest/java/com/jmeng/luadroid/ExampleInstrumentedTest.java:
--------------------------------------------------------------------------------
1 | package com.jmeng.luadroid;
2 |
3 | import android.content.Context;
4 | import android.support.test.InstrumentationRegistry;
5 | import android.support.test.runner.AndroidJUnit4;
6 |
7 | import org.junit.Test;
8 | import org.junit.runner.RunWith;
9 |
10 | import static org.junit.Assert.*;
11 |
12 | /**
13 | * Instrumentation test, which will execute on an Android device.
14 | *
15 | * @see Testing documentation
16 | */
17 | @RunWith(AndroidJUnit4.class)
18 | public class ExampleInstrumentedTest {
19 | @Test
20 | public void useAppContext() throws Exception {
21 | // Context of the app under test.
22 | Context appContext = InstrumentationRegistry.getTargetContext();
23 |
24 | assertEquals("com.jmeng.luadroid", appContext.getPackageName());
25 | }
26 | }
27 |
--------------------------------------------------------------------------------
/app/src/main/AndroidManifest.xml:
--------------------------------------------------------------------------------
1 |
2 |
4 |
5 |
12 |
13 |
14 |
15 |
16 |
17 |
18 |
19 |
20 |
21 |
--------------------------------------------------------------------------------
/app/src/main/java/com/jmengxy/luadroid/MainActivity.java:
--------------------------------------------------------------------------------
1 | package com.jmengxy.luadroid;
2 |
3 | import android.os.Bundle;
4 | import android.support.v7.app.AppCompatActivity;
5 | import android.util.Pair;
6 | import android.widget.Button;
7 | import android.widget.EditText;
8 | import android.widget.TextView;
9 | import android.widget.Toast;
10 |
11 | import com.jmengxy.lualib.Lua;
12 |
13 | public class MainActivity extends AppCompatActivity {
14 |
15 | public static final String INPUT = "input";
16 | public static final String OUTPUT = "output";
17 |
18 | private EditText inputEdit;
19 | private EditText scriptEdit;
20 | private TextView outputText;
21 | private Button executeButton;
22 |
23 | private Lua lua;
24 |
25 | @Override
26 | protected void onCreate(Bundle savedInstanceState) {
27 | super.onCreate(savedInstanceState);
28 | initUI();
29 | initLua();
30 | }
31 |
32 | private void initLua() {
33 | lua = new Lua();
34 | executeButton.setOnClickListener(v -> {
35 | lua.setString(INPUT, inputEdit.getText().toString());
36 | Pair result = lua.parseLine(scriptEdit.getText().toString());
37 | if (result.first) {
38 | String outputStr = "";
39 | switch (lua.getType(OUTPUT)) {
40 | case Lua.LUA_TYPE_NIL:
41 | outputStr = "nil";
42 | break;
43 | case Lua.LUA_TYPE_NUMBER:
44 | outputStr = lua.isInteger(OUTPUT)
45 | ? Integer.toString(lua.getInteger(OUTPUT, 0))
46 | : Double.toString(lua.getDouble(OUTPUT, 0.0));
47 | break;
48 | case Lua.LUA_TYPE_BOOLEAN:
49 | outputStr = lua.getBoolean(OUTPUT, false) ? "true" : "false";
50 | break;
51 | case Lua.LUA_TYPE_STRING:
52 | outputStr = lua.getString(OUTPUT, "");
53 | break;
54 | default:
55 | outputStr = "";
56 | break;
57 | }
58 | outputText.setText(outputStr);
59 | } else {
60 | outputText.setText("");
61 | Toast.makeText(this, "Script parse failed!", Toast.LENGTH_SHORT).show();
62 | }
63 | });
64 | }
65 |
66 | private void initUI() {
67 | setContentView(R.layout.activity_main);
68 | inputEdit = (EditText)findViewById(R.id.input);
69 | scriptEdit = (EditText)findViewById(R.id.script);
70 | outputText = (TextView) findViewById(R.id.output);
71 | executeButton = (Button) findViewById(R.id.execute);
72 | }
73 |
74 | @Override
75 | protected void onDestroy() {
76 | if (lua != null) {
77 | lua.close();
78 | lua = null;
79 | }
80 | super.onDestroy();
81 | }
82 | }
83 |
--------------------------------------------------------------------------------
/app/src/main/res/layout/activity_main.xml:
--------------------------------------------------------------------------------
1 |
2 |
6 |
10 |
14 |
18 |
22 |
26 |
30 |
35 |
36 |
37 |
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-hdpi/ic_launcher.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/jie-meng/LuaDroid/a6c3caae1a2d65c4bdcc88d2df77c45b09283e0f/app/src/main/res/mipmap-hdpi/ic_launcher.png
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-hdpi/ic_launcher_round.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/jie-meng/LuaDroid/a6c3caae1a2d65c4bdcc88d2df77c45b09283e0f/app/src/main/res/mipmap-hdpi/ic_launcher_round.png
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-mdpi/ic_launcher.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/jie-meng/LuaDroid/a6c3caae1a2d65c4bdcc88d2df77c45b09283e0f/app/src/main/res/mipmap-mdpi/ic_launcher.png
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-mdpi/ic_launcher_round.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/jie-meng/LuaDroid/a6c3caae1a2d65c4bdcc88d2df77c45b09283e0f/app/src/main/res/mipmap-mdpi/ic_launcher_round.png
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-xhdpi/ic_launcher.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/jie-meng/LuaDroid/a6c3caae1a2d65c4bdcc88d2df77c45b09283e0f/app/src/main/res/mipmap-xhdpi/ic_launcher.png
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-xhdpi/ic_launcher_round.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/jie-meng/LuaDroid/a6c3caae1a2d65c4bdcc88d2df77c45b09283e0f/app/src/main/res/mipmap-xhdpi/ic_launcher_round.png
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-xxhdpi/ic_launcher.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/jie-meng/LuaDroid/a6c3caae1a2d65c4bdcc88d2df77c45b09283e0f/app/src/main/res/mipmap-xxhdpi/ic_launcher.png
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-xxhdpi/ic_launcher_round.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/jie-meng/LuaDroid/a6c3caae1a2d65c4bdcc88d2df77c45b09283e0f/app/src/main/res/mipmap-xxhdpi/ic_launcher_round.png
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-xxxhdpi/ic_launcher.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/jie-meng/LuaDroid/a6c3caae1a2d65c4bdcc88d2df77c45b09283e0f/app/src/main/res/mipmap-xxxhdpi/ic_launcher.png
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-xxxhdpi/ic_launcher_round.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/jie-meng/LuaDroid/a6c3caae1a2d65c4bdcc88d2df77c45b09283e0f/app/src/main/res/mipmap-xxxhdpi/ic_launcher_round.png
--------------------------------------------------------------------------------
/app/src/main/res/values/colors.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 | #3F51B5
4 | #303F9F
5 | #FF4081
6 |
7 |
--------------------------------------------------------------------------------
/app/src/main/res/values/strings.xml:
--------------------------------------------------------------------------------
1 |
2 | LuaDroid
3 | input =
4 | script:
5 | output =
6 | Execute
7 |
8 |
--------------------------------------------------------------------------------
/app/src/main/res/values/styles.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
10 |
11 |
12 |
--------------------------------------------------------------------------------
/app/src/test/java/com/jmeng/luadroid/LuaDroidTest.java:
--------------------------------------------------------------------------------
1 | package com.jmeng.luadroid;
2 |
3 | import org.junit.Test;
4 |
5 | import static org.junit.Assert.assertEquals;
6 |
7 | /**
8 | * Created by jiemeng on 28/01/2018.
9 | */
10 |
11 | public class LuaDroidTest {
12 | @Test
13 | public void addition_isCorrect() throws Exception {
14 | assertEquals(4, 2 + 2);
15 | }
16 | }
17 |
--------------------------------------------------------------------------------
/build.gradle:
--------------------------------------------------------------------------------
1 | // Top-level build file where you can add configuration options common to all sub-projects/modules.
2 |
3 | buildscript {
4 | ext.support_lib_version = '26.1.0'
5 |
6 | repositories {
7 | jcenter()
8 | google()
9 | }
10 | dependencies {
11 | classpath 'com.android.tools.build:gradle:3.0.1'
12 |
13 | // NOTE: Do not place your application dependencies here; they belong
14 | // in the individual module build.gradle files
15 | }
16 | }
17 |
18 | allprojects {
19 | repositories {
20 | jcenter()
21 | google()
22 | }
23 | }
24 |
25 | task clean(type: Delete) {
26 | delete rootProject.buildDir
27 | }
28 |
--------------------------------------------------------------------------------
/gradle.properties:
--------------------------------------------------------------------------------
1 | # Project-wide Gradle settings.
2 |
3 | # IDE (e.g. Android Studio) users:
4 | # Gradle settings configured through the IDE *will override*
5 | # any settings specified in this file.
6 |
7 | # For more details on how to configure your build environment visit
8 | # http://www.gradle.org/docs/current/userguide/build_environment.html
9 |
10 | # Specifies the JVM arguments used for the daemon process.
11 | # The setting is particularly useful for tweaking memory settings.
12 | org.gradle.jvmargs=-Xmx1536m
13 |
14 | # When configured, Gradle will run in incubating parallel mode.
15 | # This option should only be used with decoupled projects. More details, visit
16 | # http://www.gradle.org/docs/current/userguide/multi_project_builds.html#sec:decoupled_projects
17 | # org.gradle.parallel=true
18 |
--------------------------------------------------------------------------------
/gradle/wrapper/gradle-wrapper.jar:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/jie-meng/LuaDroid/a6c3caae1a2d65c4bdcc88d2df77c45b09283e0f/gradle/wrapper/gradle-wrapper.jar
--------------------------------------------------------------------------------
/gradle/wrapper/gradle-wrapper.properties:
--------------------------------------------------------------------------------
1 | #Sun Jan 28 12:36:08 CST 2018
2 | distributionBase=GRADLE_USER_HOME
3 | distributionPath=wrapper/dists
4 | zipStoreBase=GRADLE_USER_HOME
5 | zipStorePath=wrapper/dists
6 | distributionUrl=https\://services.gradle.org/distributions/gradle-4.1-all.zip
7 |
--------------------------------------------------------------------------------
/gradlew:
--------------------------------------------------------------------------------
1 | #!/usr/bin/env bash
2 |
3 | ##############################################################################
4 | ##
5 | ## Gradle start up script for UN*X
6 | ##
7 | ##############################################################################
8 |
9 | # Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script.
10 | DEFAULT_JVM_OPTS=""
11 |
12 | APP_NAME="Gradle"
13 | APP_BASE_NAME=`basename "$0"`
14 |
15 | # Use the maximum available, or set MAX_FD != -1 to use that value.
16 | MAX_FD="maximum"
17 |
18 | warn ( ) {
19 | echo "$*"
20 | }
21 |
22 | die ( ) {
23 | echo
24 | echo "$*"
25 | echo
26 | exit 1
27 | }
28 |
29 | # OS specific support (must be 'true' or 'false').
30 | cygwin=false
31 | msys=false
32 | darwin=false
33 | case "`uname`" in
34 | CYGWIN* )
35 | cygwin=true
36 | ;;
37 | Darwin* )
38 | darwin=true
39 | ;;
40 | MINGW* )
41 | msys=true
42 | ;;
43 | esac
44 |
45 | # Attempt to set APP_HOME
46 | # Resolve links: $0 may be a link
47 | PRG="$0"
48 | # Need this for relative symlinks.
49 | while [ -h "$PRG" ] ; do
50 | ls=`ls -ld "$PRG"`
51 | link=`expr "$ls" : '.*-> \(.*\)$'`
52 | if expr "$link" : '/.*' > /dev/null; then
53 | PRG="$link"
54 | else
55 | PRG=`dirname "$PRG"`"/$link"
56 | fi
57 | done
58 | SAVED="`pwd`"
59 | cd "`dirname \"$PRG\"`/" >/dev/null
60 | APP_HOME="`pwd -P`"
61 | cd "$SAVED" >/dev/null
62 |
63 | CLASSPATH=$APP_HOME/gradle/wrapper/gradle-wrapper.jar
64 |
65 | # Determine the Java command to use to start the JVM.
66 | if [ -n "$JAVA_HOME" ] ; then
67 | if [ -x "$JAVA_HOME/jre/sh/java" ] ; then
68 | # IBM's JDK on AIX uses strange locations for the executables
69 | JAVACMD="$JAVA_HOME/jre/sh/java"
70 | else
71 | JAVACMD="$JAVA_HOME/bin/java"
72 | fi
73 | if [ ! -x "$JAVACMD" ] ; then
74 | die "ERROR: JAVA_HOME is set to an invalid directory: $JAVA_HOME
75 |
76 | Please set the JAVA_HOME variable in your environment to match the
77 | location of your Java installation."
78 | fi
79 | else
80 | JAVACMD="java"
81 | which java >/dev/null 2>&1 || die "ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH.
82 |
83 | Please set the JAVA_HOME variable in your environment to match the
84 | location of your Java installation."
85 | fi
86 |
87 | # Increase the maximum file descriptors if we can.
88 | if [ "$cygwin" = "false" -a "$darwin" = "false" ] ; then
89 | MAX_FD_LIMIT=`ulimit -H -n`
90 | if [ $? -eq 0 ] ; then
91 | if [ "$MAX_FD" = "maximum" -o "$MAX_FD" = "max" ] ; then
92 | MAX_FD="$MAX_FD_LIMIT"
93 | fi
94 | ulimit -n $MAX_FD
95 | if [ $? -ne 0 ] ; then
96 | warn "Could not set maximum file descriptor limit: $MAX_FD"
97 | fi
98 | else
99 | warn "Could not query maximum file descriptor limit: $MAX_FD_LIMIT"
100 | fi
101 | fi
102 |
103 | # For Darwin, add options to specify how the application appears in the dock
104 | if $darwin; then
105 | GRADLE_OPTS="$GRADLE_OPTS \"-Xdock:name=$APP_NAME\" \"-Xdock:icon=$APP_HOME/media/gradle.icns\""
106 | fi
107 |
108 | # For Cygwin, switch paths to Windows format before running java
109 | if $cygwin ; then
110 | APP_HOME=`cygpath --path --mixed "$APP_HOME"`
111 | CLASSPATH=`cygpath --path --mixed "$CLASSPATH"`
112 | JAVACMD=`cygpath --unix "$JAVACMD"`
113 |
114 | # We build the pattern for arguments to be converted via cygpath
115 | ROOTDIRSRAW=`find -L / -maxdepth 1 -mindepth 1 -type d 2>/dev/null`
116 | SEP=""
117 | for dir in $ROOTDIRSRAW ; do
118 | ROOTDIRS="$ROOTDIRS$SEP$dir"
119 | SEP="|"
120 | done
121 | OURCYGPATTERN="(^($ROOTDIRS))"
122 | # Add a user-defined pattern to the cygpath arguments
123 | if [ "$GRADLE_CYGPATTERN" != "" ] ; then
124 | OURCYGPATTERN="$OURCYGPATTERN|($GRADLE_CYGPATTERN)"
125 | fi
126 | # Now convert the arguments - kludge to limit ourselves to /bin/sh
127 | i=0
128 | for arg in "$@" ; do
129 | CHECK=`echo "$arg"|egrep -c "$OURCYGPATTERN" -`
130 | CHECK2=`echo "$arg"|egrep -c "^-"` ### Determine if an option
131 |
132 | if [ $CHECK -ne 0 ] && [ $CHECK2 -eq 0 ] ; then ### Added a condition
133 | eval `echo args$i`=`cygpath --path --ignore --mixed "$arg"`
134 | else
135 | eval `echo args$i`="\"$arg\""
136 | fi
137 | i=$((i+1))
138 | done
139 | case $i in
140 | (0) set -- ;;
141 | (1) set -- "$args0" ;;
142 | (2) set -- "$args0" "$args1" ;;
143 | (3) set -- "$args0" "$args1" "$args2" ;;
144 | (4) set -- "$args0" "$args1" "$args2" "$args3" ;;
145 | (5) set -- "$args0" "$args1" "$args2" "$args3" "$args4" ;;
146 | (6) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" ;;
147 | (7) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" ;;
148 | (8) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" "$args7" ;;
149 | (9) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" "$args7" "$args8" ;;
150 | esac
151 | fi
152 |
153 | # Split up the JVM_OPTS And GRADLE_OPTS values into an array, following the shell quoting and substitution rules
154 | function splitJvmOpts() {
155 | JVM_OPTS=("$@")
156 | }
157 | eval splitJvmOpts $DEFAULT_JVM_OPTS $JAVA_OPTS $GRADLE_OPTS
158 | JVM_OPTS[${#JVM_OPTS[*]}]="-Dorg.gradle.appname=$APP_BASE_NAME"
159 |
160 | exec "$JAVACMD" "${JVM_OPTS[@]}" -classpath "$CLASSPATH" org.gradle.wrapper.GradleWrapperMain "$@"
161 |
--------------------------------------------------------------------------------
/gradlew.bat:
--------------------------------------------------------------------------------
1 | @if "%DEBUG%" == "" @echo off
2 | @rem ##########################################################################
3 | @rem
4 | @rem Gradle startup script for Windows
5 | @rem
6 | @rem ##########################################################################
7 |
8 | @rem Set local scope for the variables with windows NT shell
9 | if "%OS%"=="Windows_NT" setlocal
10 |
11 | @rem Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script.
12 | set DEFAULT_JVM_OPTS=
13 |
14 | set DIRNAME=%~dp0
15 | if "%DIRNAME%" == "" set DIRNAME=.
16 | set APP_BASE_NAME=%~n0
17 | set APP_HOME=%DIRNAME%
18 |
19 | @rem Find java.exe
20 | if defined JAVA_HOME goto findJavaFromJavaHome
21 |
22 | set JAVA_EXE=java.exe
23 | %JAVA_EXE% -version >NUL 2>&1
24 | if "%ERRORLEVEL%" == "0" goto init
25 |
26 | echo.
27 | echo ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH.
28 | echo.
29 | echo Please set the JAVA_HOME variable in your environment to match the
30 | echo location of your Java installation.
31 |
32 | goto fail
33 |
34 | :findJavaFromJavaHome
35 | set JAVA_HOME=%JAVA_HOME:"=%
36 | set JAVA_EXE=%JAVA_HOME%/bin/java.exe
37 |
38 | if exist "%JAVA_EXE%" goto init
39 |
40 | echo.
41 | echo ERROR: JAVA_HOME is set to an invalid directory: %JAVA_HOME%
42 | echo.
43 | echo Please set the JAVA_HOME variable in your environment to match the
44 | echo location of your Java installation.
45 |
46 | goto fail
47 |
48 | :init
49 | @rem Get command-line arguments, handling Windowz variants
50 |
51 | if not "%OS%" == "Windows_NT" goto win9xME_args
52 | if "%@eval[2+2]" == "4" goto 4NT_args
53 |
54 | :win9xME_args
55 | @rem Slurp the command line arguments.
56 | set CMD_LINE_ARGS=
57 | set _SKIP=2
58 |
59 | :win9xME_args_slurp
60 | if "x%~1" == "x" goto execute
61 |
62 | set CMD_LINE_ARGS=%*
63 | goto execute
64 |
65 | :4NT_args
66 | @rem Get arguments from the 4NT Shell from JP Software
67 | set CMD_LINE_ARGS=%$
68 |
69 | :execute
70 | @rem Setup the command line
71 |
72 | set CLASSPATH=%APP_HOME%\gradle\wrapper\gradle-wrapper.jar
73 |
74 | @rem Execute Gradle
75 | "%JAVA_EXE%" %DEFAULT_JVM_OPTS% %JAVA_OPTS% %GRADLE_OPTS% "-Dorg.gradle.appname=%APP_BASE_NAME%" -classpath "%CLASSPATH%" org.gradle.wrapper.GradleWrapperMain %CMD_LINE_ARGS%
76 |
77 | :end
78 | @rem End local scope for the variables with windows NT shell
79 | if "%ERRORLEVEL%"=="0" goto mainEnd
80 |
81 | :fail
82 | rem Set variable GRADLE_EXIT_CONSOLE if you need the _script_ return code instead of
83 | rem the _cmd.exe /c_ return code!
84 | if not "" == "%GRADLE_EXIT_CONSOLE%" exit 1
85 | exit /b 1
86 |
87 | :mainEnd
88 | if "%OS%"=="Windows_NT" endlocal
89 |
90 | :omega
91 |
--------------------------------------------------------------------------------
/lualib/.gitignore:
--------------------------------------------------------------------------------
1 | /build
2 |
--------------------------------------------------------------------------------
/lualib/build.gradle:
--------------------------------------------------------------------------------
1 | apply plugin: 'com.android.library'
2 |
3 | android {
4 | compileSdkVersion 26
5 | buildToolsVersion "26.0.2"
6 |
7 | defaultConfig {
8 | minSdkVersion 15
9 | targetSdkVersion 26
10 | versionCode 1
11 | versionName "1.0"
12 | }
13 |
14 | externalNativeBuild {
15 | // Encapsulates your CMake build configurations.
16 | cmake {
17 |
18 | // Provides a relative path to your CMake build script.
19 | path "src/main/cpp/CMakeLists.txt"
20 | }
21 | }
22 | }
--------------------------------------------------------------------------------
/lualib/src/main/AndroidManifest.xml:
--------------------------------------------------------------------------------
1 |
3 |
4 |
8 |
9 |
--------------------------------------------------------------------------------
/lualib/src/main/cpp/CMakeLists.txt:
--------------------------------------------------------------------------------
1 | # Sets the minimum version of CMake required to build your native library.
2 | # This ensures that a certain set of CMake features is available to
3 | # your build.
4 |
5 | cmake_minimum_required(VERSION 3.4.1)
6 |
7 | # Specifies a library name, specifies whether the library is STATIC or
8 | # SHARED, and provides relative paths to the source code. You can
9 | # define multiple libraries by adding multiple add.library() commands,
10 | # and CMake builds them for you. When you build your app, Gradle
11 | # automatically packages shared libraries with your APK.
12 |
13 | set(LIB_NAME luax)
14 | set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11 -Wall")
15 | file(GLOB_RECURSE SRCS "${CMAKE_CURRENT_LIST_DIR}/*.cpp" "${CMAKE_CURRENT_LIST_DIR}/*.c")
16 |
17 | add_library(# Specifies the name of the library.
18 | ${LIB_NAME}
19 |
20 | # Sets the library as a shared library.
21 | SHARED
22 |
23 | # Provides a relative path to your source
24 | ${SRCS})
25 |
26 | # Include libraries needed for luax lib
27 | target_link_libraries(${LIB_NAME} android log)
28 |
--------------------------------------------------------------------------------
/lualib/src/main/cpp/lua/lapi.h:
--------------------------------------------------------------------------------
1 | /*
2 | ** $Id: lapi.h,v 2.9 2015/03/06 19:49:50 roberto Exp $
3 | ** Auxiliary functions from Lua API
4 | ** See Copyright Notice in lua.h
5 | */
6 |
7 | #ifndef lapi_h
8 | #define lapi_h
9 |
10 |
11 | #include "llimits.h"
12 | #include "lstate.h"
13 |
14 | #define api_incr_top(L) {L->top++; api_check(L, L->top <= L->ci->top, \
15 | "stack overflow");}
16 |
17 | #define adjustresults(L,nres) \
18 | { if ((nres) == LUA_MULTRET && L->ci->top < L->top) L->ci->top = L->top; }
19 |
20 | #define api_checknelems(L,n) api_check(L, (n) < (L->top - L->ci->func), \
21 | "not enough elements in the stack")
22 |
23 |
24 | #endif
25 |
--------------------------------------------------------------------------------
/lualib/src/main/cpp/lua/lauxlib.h:
--------------------------------------------------------------------------------
1 | /*
2 | ** $Id: lauxlib.h,v 1.131 2016/12/06 14:54:31 roberto Exp $
3 | ** Auxiliary functions for building Lua libraries
4 | ** See Copyright Notice in lua.h
5 | */
6 |
7 |
8 | #ifndef lauxlib_h
9 | #define lauxlib_h
10 |
11 |
12 | #include
13 | #include
14 |
15 | #include "lua.h"
16 |
17 |
18 |
19 | /* extra error code for 'luaL_loadfilex' */
20 | #define LUA_ERRFILE (LUA_ERRERR+1)
21 |
22 |
23 | /* key, in the registry, for table of loaded modules */
24 | #define LUA_LOADED_TABLE "_LOADED"
25 |
26 |
27 | /* key, in the registry, for table of preloaded loaders */
28 | #define LUA_PRELOAD_TABLE "_PRELOAD"
29 |
30 |
31 | typedef struct luaL_Reg {
32 | const char *name;
33 | lua_CFunction func;
34 | } luaL_Reg;
35 |
36 |
37 | #define LUAL_NUMSIZES (sizeof(lua_Integer)*16 + sizeof(lua_Number))
38 |
39 | LUALIB_API void (luaL_checkversion_) (lua_State *L, lua_Number ver, size_t sz);
40 | #define luaL_checkversion(L) \
41 | luaL_checkversion_(L, LUA_VERSION_NUM, LUAL_NUMSIZES)
42 |
43 | LUALIB_API int (luaL_getmetafield) (lua_State *L, int obj, const char *e);
44 | LUALIB_API int (luaL_callmeta) (lua_State *L, int obj, const char *e);
45 | LUALIB_API const char *(luaL_tolstring) (lua_State *L, int idx, size_t *len);
46 | LUALIB_API int (luaL_argerror) (lua_State *L, int arg, const char *extramsg);
47 | LUALIB_API const char *(luaL_checklstring) (lua_State *L, int arg,
48 | size_t *l);
49 | LUALIB_API const char *(luaL_optlstring) (lua_State *L, int arg,
50 | const char *def, size_t *l);
51 | LUALIB_API lua_Number (luaL_checknumber) (lua_State *L, int arg);
52 | LUALIB_API lua_Number (luaL_optnumber) (lua_State *L, int arg, lua_Number def);
53 |
54 | LUALIB_API lua_Integer (luaL_checkinteger) (lua_State *L, int arg);
55 | LUALIB_API lua_Integer (luaL_optinteger) (lua_State *L, int arg,
56 | lua_Integer def);
57 |
58 | LUALIB_API void (luaL_checkstack) (lua_State *L, int sz, const char *msg);
59 | LUALIB_API void (luaL_checktype) (lua_State *L, int arg, int t);
60 | LUALIB_API void (luaL_checkany) (lua_State *L, int arg);
61 |
62 | LUALIB_API int (luaL_newmetatable) (lua_State *L, const char *tname);
63 | LUALIB_API void (luaL_setmetatable) (lua_State *L, const char *tname);
64 | LUALIB_API void *(luaL_testudata) (lua_State *L, int ud, const char *tname);
65 | LUALIB_API void *(luaL_checkudata) (lua_State *L, int ud, const char *tname);
66 |
67 | LUALIB_API void (luaL_where) (lua_State *L, int lvl);
68 | LUALIB_API int (luaL_error) (lua_State *L, const char *fmt, ...);
69 |
70 | LUALIB_API int (luaL_checkoption) (lua_State *L, int arg, const char *def,
71 | const char *const lst[]);
72 |
73 | LUALIB_API int (luaL_fileresult) (lua_State *L, int stat, const char *fname);
74 | LUALIB_API int (luaL_execresult) (lua_State *L, int stat);
75 |
76 | /* predefined references */
77 | #define LUA_NOREF (-2)
78 | #define LUA_REFNIL (-1)
79 |
80 | LUALIB_API int (luaL_ref) (lua_State *L, int t);
81 | LUALIB_API void (luaL_unref) (lua_State *L, int t, int ref);
82 |
83 | LUALIB_API int (luaL_loadfilex) (lua_State *L, const char *filename,
84 | const char *mode);
85 |
86 | #define luaL_loadfile(L,f) luaL_loadfilex(L,f,NULL)
87 |
88 | LUALIB_API int (luaL_loadbufferx) (lua_State *L, const char *buff, size_t sz,
89 | const char *name, const char *mode);
90 | LUALIB_API int (luaL_loadstring) (lua_State *L, const char *s);
91 |
92 | LUALIB_API lua_State *(luaL_newstate) (void);
93 |
94 | LUALIB_API lua_Integer (luaL_len) (lua_State *L, int idx);
95 |
96 | LUALIB_API const char *(luaL_gsub) (lua_State *L, const char *s, const char *p,
97 | const char *r);
98 |
99 | LUALIB_API void (luaL_setfuncs) (lua_State *L, const luaL_Reg *l, int nup);
100 |
101 | LUALIB_API int (luaL_getsubtable) (lua_State *L, int idx, const char *fname);
102 |
103 | LUALIB_API void (luaL_traceback) (lua_State *L, lua_State *L1,
104 | const char *msg, int level);
105 |
106 | LUALIB_API void (luaL_requiref) (lua_State *L, const char *modname,
107 | lua_CFunction openf, int glb);
108 |
109 | /*
110 | ** ===============================================================
111 | ** some useful macros
112 | ** ===============================================================
113 | */
114 |
115 |
116 | #define luaL_newlibtable(L,l) \
117 | lua_createtable(L, 0, sizeof(l)/sizeof((l)[0]) - 1)
118 |
119 | #define luaL_newlib(L,l) \
120 | (luaL_checkversion(L), luaL_newlibtable(L,l), luaL_setfuncs(L,l,0))
121 |
122 | #define luaL_argcheck(L, cond,arg,extramsg) \
123 | ((void)((cond) || luaL_argerror(L, (arg), (extramsg))))
124 | #define luaL_checkstring(L,n) (luaL_checklstring(L, (n), NULL))
125 | #define luaL_optstring(L,n,d) (luaL_optlstring(L, (n), (d), NULL))
126 |
127 | #define luaL_typename(L,i) lua_typename(L, lua_type(L,(i)))
128 |
129 | #define luaL_dofile(L, fn) \
130 | (luaL_loadfile(L, fn) || lua_pcall(L, 0, LUA_MULTRET, 0))
131 |
132 | #define luaL_dostring(L, s) \
133 | (luaL_loadstring(L, s) || lua_pcall(L, 0, LUA_MULTRET, 0))
134 |
135 | #define luaL_getmetatable(L,n) (lua_getfield(L, LUA_REGISTRYINDEX, (n)))
136 |
137 | #define luaL_opt(L,f,n,d) (lua_isnoneornil(L,(n)) ? (d) : f(L,(n)))
138 |
139 | #define luaL_loadbuffer(L,s,sz,n) luaL_loadbufferx(L,s,sz,n,NULL)
140 |
141 |
142 | /*
143 | ** {======================================================
144 | ** Generic Buffer manipulation
145 | ** =======================================================
146 | */
147 |
148 | typedef struct luaL_Buffer {
149 | char *b; /* buffer address */
150 | size_t size; /* buffer size */
151 | size_t n; /* number of characters in buffer */
152 | lua_State *L;
153 | char initb[LUAL_BUFFERSIZE]; /* initial buffer */
154 | } luaL_Buffer;
155 |
156 |
157 | #define luaL_addchar(B,c) \
158 | ((void)((B)->n < (B)->size || luaL_prepbuffsize((B), 1)), \
159 | ((B)->b[(B)->n++] = (c)))
160 |
161 | #define luaL_addsize(B,s) ((B)->n += (s))
162 |
163 | LUALIB_API void (luaL_buffinit) (lua_State *L, luaL_Buffer *B);
164 | LUALIB_API char *(luaL_prepbuffsize) (luaL_Buffer *B, size_t sz);
165 | LUALIB_API void (luaL_addlstring) (luaL_Buffer *B, const char *s, size_t l);
166 | LUALIB_API void (luaL_addstring) (luaL_Buffer *B, const char *s);
167 | LUALIB_API void (luaL_addvalue) (luaL_Buffer *B);
168 | LUALIB_API void (luaL_pushresult) (luaL_Buffer *B);
169 | LUALIB_API void (luaL_pushresultsize) (luaL_Buffer *B, size_t sz);
170 | LUALIB_API char *(luaL_buffinitsize) (lua_State *L, luaL_Buffer *B, size_t sz);
171 |
172 | #define luaL_prepbuffer(B) luaL_prepbuffsize(B, LUAL_BUFFERSIZE)
173 |
174 | /* }====================================================== */
175 |
176 |
177 |
178 | /*
179 | ** {======================================================
180 | ** File handles for IO library
181 | ** =======================================================
182 | */
183 |
184 | /*
185 | ** A file handle is a userdata with metatable 'LUA_FILEHANDLE' and
186 | ** initial structure 'luaL_Stream' (it may contain other fields
187 | ** after that initial structure).
188 | */
189 |
190 | #define LUA_FILEHANDLE "FILE*"
191 |
192 |
193 | typedef struct luaL_Stream {
194 | FILE *f; /* stream (NULL for incompletely created streams) */
195 | lua_CFunction closef; /* to close stream (NULL for closed streams) */
196 | } luaL_Stream;
197 |
198 | /* }====================================================== */
199 |
200 |
201 |
202 | /* compatibility with old module system */
203 | #if defined(LUA_COMPAT_MODULE)
204 |
205 | LUALIB_API void (luaL_pushmodule) (lua_State *L, const char *modname,
206 | int sizehint);
207 | LUALIB_API void (luaL_openlib) (lua_State *L, const char *libname,
208 | const luaL_Reg *l, int nup);
209 |
210 | #define luaL_register(L,n,l) (luaL_openlib(L,(n),(l),0))
211 |
212 | #endif
213 |
214 |
215 | /*
216 | ** {==================================================================
217 | ** "Abstraction Layer" for basic report of messages and errors
218 | ** ===================================================================
219 | */
220 |
221 | /* print a string */
222 | #if !defined(lua_writestring)
223 | #define lua_writestring(s,l) fwrite((s), sizeof(char), (l), stdout)
224 | #endif
225 |
226 | /* print a newline and flush the output */
227 | #if !defined(lua_writeline)
228 | #define lua_writeline() (lua_writestring("\n", 1), fflush(stdout))
229 | #endif
230 |
231 | /* print an error message */
232 | #if !defined(lua_writestringerror)
233 | #define lua_writestringerror(s,p) \
234 | (fprintf(stderr, (s), (p)), fflush(stderr))
235 | #endif
236 |
237 | /* }================================================================== */
238 |
239 |
240 | /*
241 | ** {============================================================
242 | ** Compatibility with deprecated conversions
243 | ** =============================================================
244 | */
245 | #if defined(LUA_COMPAT_APIINTCASTS)
246 |
247 | #define luaL_checkunsigned(L,a) ((lua_Unsigned)luaL_checkinteger(L,a))
248 | #define luaL_optunsigned(L,a,d) \
249 | ((lua_Unsigned)luaL_optinteger(L,a,(lua_Integer)(d)))
250 |
251 | #define luaL_checkint(L,n) ((int)luaL_checkinteger(L, (n)))
252 | #define luaL_optint(L,n,d) ((int)luaL_optinteger(L, (n), (d)))
253 |
254 | #define luaL_checklong(L,n) ((long)luaL_checkinteger(L, (n)))
255 | #define luaL_optlong(L,n,d) ((long)luaL_optinteger(L, (n), (d)))
256 |
257 | #endif
258 | /* }============================================================ */
259 |
260 |
261 |
262 | #endif
263 |
264 |
265 |
--------------------------------------------------------------------------------
/lualib/src/main/cpp/lua/lbitlib.c:
--------------------------------------------------------------------------------
1 | /*
2 | ** $Id: lbitlib.c,v 1.30 2015/11/11 19:08:09 roberto Exp $
3 | ** Standard library for bitwise operations
4 | ** See Copyright Notice in lua.h
5 | */
6 |
7 | #define lbitlib_c
8 | #define LUA_LIB
9 |
10 | #include "lprefix.h"
11 |
12 |
13 | #include "lua.h"
14 |
15 | #include "lauxlib.h"
16 | #include "lualib.h"
17 |
18 |
19 | #if defined(LUA_COMPAT_BITLIB) /* { */
20 |
21 |
22 | #define pushunsigned(L,n) lua_pushinteger(L, (lua_Integer)(n))
23 | #define checkunsigned(L,i) ((lua_Unsigned)luaL_checkinteger(L,i))
24 |
25 |
26 | /* number of bits to consider in a number */
27 | #if !defined(LUA_NBITS)
28 | #define LUA_NBITS 32
29 | #endif
30 |
31 |
32 | /*
33 | ** a lua_Unsigned with its first LUA_NBITS bits equal to 1. (Shift must
34 | ** be made in two parts to avoid problems when LUA_NBITS is equal to the
35 | ** number of bits in a lua_Unsigned.)
36 | */
37 | #define ALLONES (~(((~(lua_Unsigned)0) << (LUA_NBITS - 1)) << 1))
38 |
39 |
40 | /* macro to trim extra bits */
41 | #define trim(x) ((x) & ALLONES)
42 |
43 |
44 | /* builds a number with 'n' ones (1 <= n <= LUA_NBITS) */
45 | #define mask(n) (~((ALLONES << 1) << ((n) - 1)))
46 |
47 |
48 |
49 | static lua_Unsigned andaux (lua_State *L) {
50 | int i, n = lua_gettop(L);
51 | lua_Unsigned r = ~(lua_Unsigned)0;
52 | for (i = 1; i <= n; i++)
53 | r &= checkunsigned(L, i);
54 | return trim(r);
55 | }
56 |
57 |
58 | static int b_and (lua_State *L) {
59 | lua_Unsigned r = andaux(L);
60 | pushunsigned(L, r);
61 | return 1;
62 | }
63 |
64 |
65 | static int b_test (lua_State *L) {
66 | lua_Unsigned r = andaux(L);
67 | lua_pushboolean(L, r != 0);
68 | return 1;
69 | }
70 |
71 |
72 | static int b_or (lua_State *L) {
73 | int i, n = lua_gettop(L);
74 | lua_Unsigned r = 0;
75 | for (i = 1; i <= n; i++)
76 | r |= checkunsigned(L, i);
77 | pushunsigned(L, trim(r));
78 | return 1;
79 | }
80 |
81 |
82 | static int b_xor (lua_State *L) {
83 | int i, n = lua_gettop(L);
84 | lua_Unsigned r = 0;
85 | for (i = 1; i <= n; i++)
86 | r ^= checkunsigned(L, i);
87 | pushunsigned(L, trim(r));
88 | return 1;
89 | }
90 |
91 |
92 | static int b_not (lua_State *L) {
93 | lua_Unsigned r = ~checkunsigned(L, 1);
94 | pushunsigned(L, trim(r));
95 | return 1;
96 | }
97 |
98 |
99 | static int b_shift (lua_State *L, lua_Unsigned r, lua_Integer i) {
100 | if (i < 0) { /* shift right? */
101 | i = -i;
102 | r = trim(r);
103 | if (i >= LUA_NBITS) r = 0;
104 | else r >>= i;
105 | }
106 | else { /* shift left */
107 | if (i >= LUA_NBITS) r = 0;
108 | else r <<= i;
109 | r = trim(r);
110 | }
111 | pushunsigned(L, r);
112 | return 1;
113 | }
114 |
115 |
116 | static int b_lshift (lua_State *L) {
117 | return b_shift(L, checkunsigned(L, 1), luaL_checkinteger(L, 2));
118 | }
119 |
120 |
121 | static int b_rshift (lua_State *L) {
122 | return b_shift(L, checkunsigned(L, 1), -luaL_checkinteger(L, 2));
123 | }
124 |
125 |
126 | static int b_arshift (lua_State *L) {
127 | lua_Unsigned r = checkunsigned(L, 1);
128 | lua_Integer i = luaL_checkinteger(L, 2);
129 | if (i < 0 || !(r & ((lua_Unsigned)1 << (LUA_NBITS - 1))))
130 | return b_shift(L, r, -i);
131 | else { /* arithmetic shift for 'negative' number */
132 | if (i >= LUA_NBITS) r = ALLONES;
133 | else
134 | r = trim((r >> i) | ~(trim(~(lua_Unsigned)0) >> i)); /* add signal bit */
135 | pushunsigned(L, r);
136 | return 1;
137 | }
138 | }
139 |
140 |
141 | static int b_rot (lua_State *L, lua_Integer d) {
142 | lua_Unsigned r = checkunsigned(L, 1);
143 | int i = d & (LUA_NBITS - 1); /* i = d % NBITS */
144 | r = trim(r);
145 | if (i != 0) /* avoid undefined shift of LUA_NBITS when i == 0 */
146 | r = (r << i) | (r >> (LUA_NBITS - i));
147 | pushunsigned(L, trim(r));
148 | return 1;
149 | }
150 |
151 |
152 | static int b_lrot (lua_State *L) {
153 | return b_rot(L, luaL_checkinteger(L, 2));
154 | }
155 |
156 |
157 | static int b_rrot (lua_State *L) {
158 | return b_rot(L, -luaL_checkinteger(L, 2));
159 | }
160 |
161 |
162 | /*
163 | ** get field and width arguments for field-manipulation functions,
164 | ** checking whether they are valid.
165 | ** ('luaL_error' called without 'return' to avoid later warnings about
166 | ** 'width' being used uninitialized.)
167 | */
168 | static int fieldargs (lua_State *L, int farg, int *width) {
169 | lua_Integer f = luaL_checkinteger(L, farg);
170 | lua_Integer w = luaL_optinteger(L, farg + 1, 1);
171 | luaL_argcheck(L, 0 <= f, farg, "field cannot be negative");
172 | luaL_argcheck(L, 0 < w, farg + 1, "width must be positive");
173 | if (f + w > LUA_NBITS)
174 | luaL_error(L, "trying to access non-existent bits");
175 | *width = (int)w;
176 | return (int)f;
177 | }
178 |
179 |
180 | static int b_extract (lua_State *L) {
181 | int w;
182 | lua_Unsigned r = trim(checkunsigned(L, 1));
183 | int f = fieldargs(L, 2, &w);
184 | r = (r >> f) & mask(w);
185 | pushunsigned(L, r);
186 | return 1;
187 | }
188 |
189 |
190 | static int b_replace (lua_State *L) {
191 | int w;
192 | lua_Unsigned r = trim(checkunsigned(L, 1));
193 | lua_Unsigned v = trim(checkunsigned(L, 2));
194 | int f = fieldargs(L, 3, &w);
195 | lua_Unsigned m = mask(w);
196 | r = (r & ~(m << f)) | ((v & m) << f);
197 | pushunsigned(L, r);
198 | return 1;
199 | }
200 |
201 |
202 | static const luaL_Reg bitlib[] = {
203 | {"arshift", b_arshift},
204 | {"band", b_and},
205 | {"bnot", b_not},
206 | {"bor", b_or},
207 | {"bxor", b_xor},
208 | {"btest", b_test},
209 | {"extract", b_extract},
210 | {"lrotate", b_lrot},
211 | {"lshift", b_lshift},
212 | {"replace", b_replace},
213 | {"rrotate", b_rrot},
214 | {"rshift", b_rshift},
215 | {NULL, NULL}
216 | };
217 |
218 |
219 |
220 | LUAMOD_API int luaopen_bit32 (lua_State *L) {
221 | luaL_newlib(L, bitlib);
222 | return 1;
223 | }
224 |
225 |
226 | #else /* }{ */
227 |
228 |
229 | LUAMOD_API int luaopen_bit32 (lua_State *L) {
230 | return luaL_error(L, "library 'bit32' has been deprecated");
231 | }
232 |
233 | #endif /* } */
234 |
--------------------------------------------------------------------------------
/lualib/src/main/cpp/lua/lcode.h:
--------------------------------------------------------------------------------
1 | /*
2 | ** $Id: lcode.h,v 1.64 2016/01/05 16:22:37 roberto Exp $
3 | ** Code generator for Lua
4 | ** See Copyright Notice in lua.h
5 | */
6 |
7 | #ifndef lcode_h
8 | #define lcode_h
9 |
10 | #include "llex.h"
11 | #include "lobject.h"
12 | #include "lopcodes.h"
13 | #include "lparser.h"
14 |
15 |
16 | /*
17 | ** Marks the end of a patch list. It is an invalid value both as an absolute
18 | ** address, and as a list link (would link an element to itself).
19 | */
20 | #define NO_JUMP (-1)
21 |
22 |
23 | /*
24 | ** grep "ORDER OPR" if you change these enums (ORDER OP)
25 | */
26 | typedef enum BinOpr {
27 | OPR_ADD, OPR_SUB, OPR_MUL, OPR_MOD, OPR_POW,
28 | OPR_DIV,
29 | OPR_IDIV,
30 | OPR_BAND, OPR_BOR, OPR_BXOR,
31 | OPR_SHL, OPR_SHR,
32 | OPR_CONCAT,
33 | OPR_EQ, OPR_LT, OPR_LE,
34 | OPR_NE, OPR_GT, OPR_GE,
35 | OPR_AND, OPR_OR,
36 | OPR_NOBINOPR
37 | } BinOpr;
38 |
39 |
40 | typedef enum UnOpr { OPR_MINUS, OPR_BNOT, OPR_NOT, OPR_LEN, OPR_NOUNOPR } UnOpr;
41 |
42 |
43 | /* get (pointer to) instruction of given 'expdesc' */
44 | #define getinstruction(fs,e) ((fs)->f->code[(e)->u.info])
45 |
46 | #define luaK_codeAsBx(fs,o,A,sBx) luaK_codeABx(fs,o,A,(sBx)+MAXARG_sBx)
47 |
48 | #define luaK_setmultret(fs,e) luaK_setreturns(fs, e, LUA_MULTRET)
49 |
50 | #define luaK_jumpto(fs,t) luaK_patchlist(fs, luaK_jump(fs), t)
51 |
52 | LUAI_FUNC int luaK_codeABx (FuncState *fs, OpCode o, int A, unsigned int Bx);
53 | LUAI_FUNC int luaK_codeABC (FuncState *fs, OpCode o, int A, int B, int C);
54 | LUAI_FUNC int luaK_codek (FuncState *fs, int reg, int k);
55 | LUAI_FUNC void luaK_fixline (FuncState *fs, int line);
56 | LUAI_FUNC void luaK_nil (FuncState *fs, int from, int n);
57 | LUAI_FUNC void luaK_reserveregs (FuncState *fs, int n);
58 | LUAI_FUNC void luaK_checkstack (FuncState *fs, int n);
59 | LUAI_FUNC int luaK_stringK (FuncState *fs, TString *s);
60 | LUAI_FUNC int luaK_intK (FuncState *fs, lua_Integer n);
61 | LUAI_FUNC void luaK_dischargevars (FuncState *fs, expdesc *e);
62 | LUAI_FUNC int luaK_exp2anyreg (FuncState *fs, expdesc *e);
63 | LUAI_FUNC void luaK_exp2anyregup (FuncState *fs, expdesc *e);
64 | LUAI_FUNC void luaK_exp2nextreg (FuncState *fs, expdesc *e);
65 | LUAI_FUNC void luaK_exp2val (FuncState *fs, expdesc *e);
66 | LUAI_FUNC int luaK_exp2RK (FuncState *fs, expdesc *e);
67 | LUAI_FUNC void luaK_self (FuncState *fs, expdesc *e, expdesc *key);
68 | LUAI_FUNC void luaK_indexed (FuncState *fs, expdesc *t, expdesc *k);
69 | LUAI_FUNC void luaK_goiftrue (FuncState *fs, expdesc *e);
70 | LUAI_FUNC void luaK_goiffalse (FuncState *fs, expdesc *e);
71 | LUAI_FUNC void luaK_storevar (FuncState *fs, expdesc *var, expdesc *e);
72 | LUAI_FUNC void luaK_setreturns (FuncState *fs, expdesc *e, int nresults);
73 | LUAI_FUNC void luaK_setoneret (FuncState *fs, expdesc *e);
74 | LUAI_FUNC int luaK_jump (FuncState *fs);
75 | LUAI_FUNC void luaK_ret (FuncState *fs, int first, int nret);
76 | LUAI_FUNC void luaK_patchlist (FuncState *fs, int list, int target);
77 | LUAI_FUNC void luaK_patchtohere (FuncState *fs, int list);
78 | LUAI_FUNC void luaK_patchclose (FuncState *fs, int list, int level);
79 | LUAI_FUNC void luaK_concat (FuncState *fs, int *l1, int l2);
80 | LUAI_FUNC int luaK_getlabel (FuncState *fs);
81 | LUAI_FUNC void luaK_prefix (FuncState *fs, UnOpr op, expdesc *v, int line);
82 | LUAI_FUNC void luaK_infix (FuncState *fs, BinOpr op, expdesc *v);
83 | LUAI_FUNC void luaK_posfix (FuncState *fs, BinOpr op, expdesc *v1,
84 | expdesc *v2, int line);
85 | LUAI_FUNC void luaK_setlist (FuncState *fs, int base, int nelems, int tostore);
86 |
87 |
88 | #endif
89 |
--------------------------------------------------------------------------------
/lualib/src/main/cpp/lua/lcorolib.c:
--------------------------------------------------------------------------------
1 | /*
2 | ** $Id: lcorolib.c,v 1.10 2016/04/11 19:19:55 roberto Exp $
3 | ** Coroutine Library
4 | ** See Copyright Notice in lua.h
5 | */
6 |
7 | #define lcorolib_c
8 | #define LUA_LIB
9 |
10 | #include "lprefix.h"
11 |
12 |
13 | #include
14 |
15 | #include "lua.h"
16 |
17 | #include "lauxlib.h"
18 | #include "lualib.h"
19 |
20 |
21 | static lua_State *getco (lua_State *L) {
22 | lua_State *co = lua_tothread(L, 1);
23 | luaL_argcheck(L, co, 1, "thread expected");
24 | return co;
25 | }
26 |
27 |
28 | static int auxresume (lua_State *L, lua_State *co, int narg) {
29 | int status;
30 | if (!lua_checkstack(co, narg)) {
31 | lua_pushliteral(L, "too many arguments to resume");
32 | return -1; /* error flag */
33 | }
34 | if (lua_status(co) == LUA_OK && lua_gettop(co) == 0) {
35 | lua_pushliteral(L, "cannot resume dead coroutine");
36 | return -1; /* error flag */
37 | }
38 | lua_xmove(L, co, narg);
39 | status = lua_resume(co, L, narg);
40 | if (status == LUA_OK || status == LUA_YIELD) {
41 | int nres = lua_gettop(co);
42 | if (!lua_checkstack(L, nres + 1)) {
43 | lua_pop(co, nres); /* remove results anyway */
44 | lua_pushliteral(L, "too many results to resume");
45 | return -1; /* error flag */
46 | }
47 | lua_xmove(co, L, nres); /* move yielded values */
48 | return nres;
49 | }
50 | else {
51 | lua_xmove(co, L, 1); /* move error message */
52 | return -1; /* error flag */
53 | }
54 | }
55 |
56 |
57 | static int luaB_coresume (lua_State *L) {
58 | lua_State *co = getco(L);
59 | int r;
60 | r = auxresume(L, co, lua_gettop(L) - 1);
61 | if (r < 0) {
62 | lua_pushboolean(L, 0);
63 | lua_insert(L, -2);
64 | return 2; /* return false + error message */
65 | }
66 | else {
67 | lua_pushboolean(L, 1);
68 | lua_insert(L, -(r + 1));
69 | return r + 1; /* return true + 'resume' returns */
70 | }
71 | }
72 |
73 |
74 | static int luaB_auxwrap (lua_State *L) {
75 | lua_State *co = lua_tothread(L, lua_upvalueindex(1));
76 | int r = auxresume(L, co, lua_gettop(L));
77 | if (r < 0) {
78 | if (lua_type(L, -1) == LUA_TSTRING) { /* error object is a string? */
79 | luaL_where(L, 1); /* add extra info */
80 | lua_insert(L, -2);
81 | lua_concat(L, 2);
82 | }
83 | return lua_error(L); /* propagate error */
84 | }
85 | return r;
86 | }
87 |
88 |
89 | static int luaB_cocreate (lua_State *L) {
90 | lua_State *NL;
91 | luaL_checktype(L, 1, LUA_TFUNCTION);
92 | NL = lua_newthread(L);
93 | lua_pushvalue(L, 1); /* move function to top */
94 | lua_xmove(L, NL, 1); /* move function from L to NL */
95 | return 1;
96 | }
97 |
98 |
99 | static int luaB_cowrap (lua_State *L) {
100 | luaB_cocreate(L);
101 | lua_pushcclosure(L, luaB_auxwrap, 1);
102 | return 1;
103 | }
104 |
105 |
106 | static int luaB_yield (lua_State *L) {
107 | return lua_yield(L, lua_gettop(L));
108 | }
109 |
110 |
111 | static int luaB_costatus (lua_State *L) {
112 | lua_State *co = getco(L);
113 | if (L == co) lua_pushliteral(L, "running");
114 | else {
115 | switch (lua_status(co)) {
116 | case LUA_YIELD:
117 | lua_pushliteral(L, "suspended");
118 | break;
119 | case LUA_OK: {
120 | lua_Debug ar;
121 | if (lua_getstack(co, 0, &ar) > 0) /* does it have frames? */
122 | lua_pushliteral(L, "normal"); /* it is running */
123 | else if (lua_gettop(co) == 0)
124 | lua_pushliteral(L, "dead");
125 | else
126 | lua_pushliteral(L, "suspended"); /* initial state */
127 | break;
128 | }
129 | default: /* some error occurred */
130 | lua_pushliteral(L, "dead");
131 | break;
132 | }
133 | }
134 | return 1;
135 | }
136 |
137 |
138 | static int luaB_yieldable (lua_State *L) {
139 | lua_pushboolean(L, lua_isyieldable(L));
140 | return 1;
141 | }
142 |
143 |
144 | static int luaB_corunning (lua_State *L) {
145 | int ismain = lua_pushthread(L);
146 | lua_pushboolean(L, ismain);
147 | return 2;
148 | }
149 |
150 |
151 | static const luaL_Reg co_funcs[] = {
152 | {"create", luaB_cocreate},
153 | {"resume", luaB_coresume},
154 | {"running", luaB_corunning},
155 | {"status", luaB_costatus},
156 | {"wrap", luaB_cowrap},
157 | {"yield", luaB_yield},
158 | {"isyieldable", luaB_yieldable},
159 | {NULL, NULL}
160 | };
161 |
162 |
163 |
164 | LUAMOD_API int luaopen_coroutine (lua_State *L) {
165 | luaL_newlib(L, co_funcs);
166 | return 1;
167 | }
168 |
169 |
--------------------------------------------------------------------------------
/lualib/src/main/cpp/lua/lctype.c:
--------------------------------------------------------------------------------
1 | /*
2 | ** $Id: lctype.c,v 1.12 2014/11/02 19:19:04 roberto Exp $
3 | ** 'ctype' functions for Lua
4 | ** See Copyright Notice in lua.h
5 | */
6 |
7 | #define lctype_c
8 | #define LUA_CORE
9 |
10 | #include "lprefix.h"
11 |
12 |
13 | #include "lctype.h"
14 |
15 | #if !LUA_USE_CTYPE /* { */
16 |
17 | #include
18 |
19 | LUAI_DDEF const lu_byte luai_ctype_[UCHAR_MAX + 2] = {
20 | 0x00, /* EOZ */
21 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* 0. */
22 | 0x00, 0x08, 0x08, 0x08, 0x08, 0x08, 0x00, 0x00,
23 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* 1. */
24 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
25 | 0x0c, 0x04, 0x04, 0x04, 0x04, 0x04, 0x04, 0x04, /* 2. */
26 | 0x04, 0x04, 0x04, 0x04, 0x04, 0x04, 0x04, 0x04,
27 | 0x16, 0x16, 0x16, 0x16, 0x16, 0x16, 0x16, 0x16, /* 3. */
28 | 0x16, 0x16, 0x04, 0x04, 0x04, 0x04, 0x04, 0x04,
29 | 0x04, 0x15, 0x15, 0x15, 0x15, 0x15, 0x15, 0x05, /* 4. */
30 | 0x05, 0x05, 0x05, 0x05, 0x05, 0x05, 0x05, 0x05,
31 | 0x05, 0x05, 0x05, 0x05, 0x05, 0x05, 0x05, 0x05, /* 5. */
32 | 0x05, 0x05, 0x05, 0x04, 0x04, 0x04, 0x04, 0x05,
33 | 0x04, 0x15, 0x15, 0x15, 0x15, 0x15, 0x15, 0x05, /* 6. */
34 | 0x05, 0x05, 0x05, 0x05, 0x05, 0x05, 0x05, 0x05,
35 | 0x05, 0x05, 0x05, 0x05, 0x05, 0x05, 0x05, 0x05, /* 7. */
36 | 0x05, 0x05, 0x05, 0x04, 0x04, 0x04, 0x04, 0x00,
37 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* 8. */
38 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
39 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* 9. */
40 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
41 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* a. */
42 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
43 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* b. */
44 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
45 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* c. */
46 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
47 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* d. */
48 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
49 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* e. */
50 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
51 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* f. */
52 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
53 | };
54 |
55 | #endif /* } */
56 |
--------------------------------------------------------------------------------
/lualib/src/main/cpp/lua/lctype.h:
--------------------------------------------------------------------------------
1 | /*
2 | ** $Id: lctype.h,v 1.12 2011/07/15 12:50:29 roberto Exp $
3 | ** 'ctype' functions for Lua
4 | ** See Copyright Notice in lua.h
5 | */
6 |
7 | #ifndef lctype_h
8 | #define lctype_h
9 |
10 | #include "lua.h"
11 |
12 |
13 | /*
14 | ** WARNING: the functions defined here do not necessarily correspond
15 | ** to the similar functions in the standard C ctype.h. They are
16 | ** optimized for the specific needs of Lua
17 | */
18 |
19 | #if !defined(LUA_USE_CTYPE)
20 |
21 | #if 'A' == 65 && '0' == 48
22 | /* ASCII case: can use its own tables; faster and fixed */
23 | #define LUA_USE_CTYPE 0
24 | #else
25 | /* must use standard C ctype */
26 | #define LUA_USE_CTYPE 1
27 | #endif
28 |
29 | #endif
30 |
31 |
32 | #if !LUA_USE_CTYPE /* { */
33 |
34 | #include
35 |
36 | #include "llimits.h"
37 |
38 |
39 | #define ALPHABIT 0
40 | #define DIGITBIT 1
41 | #define PRINTBIT 2
42 | #define SPACEBIT 3
43 | #define XDIGITBIT 4
44 |
45 |
46 | #define MASK(B) (1 << (B))
47 |
48 |
49 | /*
50 | ** add 1 to char to allow index -1 (EOZ)
51 | */
52 | #define testprop(c,p) (luai_ctype_[(c)+1] & (p))
53 |
54 | /*
55 | ** 'lalpha' (Lua alphabetic) and 'lalnum' (Lua alphanumeric) both include '_'
56 | */
57 | #define lislalpha(c) testprop(c, MASK(ALPHABIT))
58 | #define lislalnum(c) testprop(c, (MASK(ALPHABIT) | MASK(DIGITBIT)))
59 | #define lisdigit(c) testprop(c, MASK(DIGITBIT))
60 | #define lisspace(c) testprop(c, MASK(SPACEBIT))
61 | #define lisprint(c) testprop(c, MASK(PRINTBIT))
62 | #define lisxdigit(c) testprop(c, MASK(XDIGITBIT))
63 |
64 | /*
65 | ** this 'ltolower' only works for alphabetic characters
66 | */
67 | #define ltolower(c) ((c) | ('A' ^ 'a'))
68 |
69 |
70 | /* two more entries for 0 and -1 (EOZ) */
71 | LUAI_DDEC const lu_byte luai_ctype_[UCHAR_MAX + 2];
72 |
73 |
74 | #else /* }{ */
75 |
76 | /*
77 | ** use standard C ctypes
78 | */
79 |
80 | #include
81 |
82 |
83 | #define lislalpha(c) (isalpha(c) || (c) == '_')
84 | #define lislalnum(c) (isalnum(c) || (c) == '_')
85 | #define lisdigit(c) (isdigit(c))
86 | #define lisspace(c) (isspace(c))
87 | #define lisprint(c) (isprint(c))
88 | #define lisxdigit(c) (isxdigit(c))
89 |
90 | #define ltolower(c) (tolower(c))
91 |
92 | #endif /* } */
93 |
94 | #endif
95 |
96 |
--------------------------------------------------------------------------------
/lualib/src/main/cpp/lua/ldebug.h:
--------------------------------------------------------------------------------
1 | /*
2 | ** $Id: ldebug.h,v 2.14 2015/05/22 17:45:56 roberto Exp $
3 | ** Auxiliary functions from Debug Interface module
4 | ** See Copyright Notice in lua.h
5 | */
6 |
7 | #ifndef ldebug_h
8 | #define ldebug_h
9 |
10 |
11 | #include "lstate.h"
12 |
13 |
14 | #define pcRel(pc, p) (cast(int, (pc) - (p)->code) - 1)
15 |
16 | #define getfuncline(f,pc) (((f)->lineinfo) ? (f)->lineinfo[pc] : -1)
17 |
18 | #define resethookcount(L) (L->hookcount = L->basehookcount)
19 |
20 |
21 | LUAI_FUNC l_noret luaG_typeerror (lua_State *L, const TValue *o,
22 | const char *opname);
23 | LUAI_FUNC l_noret luaG_concaterror (lua_State *L, const TValue *p1,
24 | const TValue *p2);
25 | LUAI_FUNC l_noret luaG_opinterror (lua_State *L, const TValue *p1,
26 | const TValue *p2,
27 | const char *msg);
28 | LUAI_FUNC l_noret luaG_tointerror (lua_State *L, const TValue *p1,
29 | const TValue *p2);
30 | LUAI_FUNC l_noret luaG_ordererror (lua_State *L, const TValue *p1,
31 | const TValue *p2);
32 | LUAI_FUNC l_noret luaG_runerror (lua_State *L, const char *fmt, ...);
33 | LUAI_FUNC const char *luaG_addinfo (lua_State *L, const char *msg,
34 | TString *src, int line);
35 | LUAI_FUNC l_noret luaG_errormsg (lua_State *L);
36 | LUAI_FUNC void luaG_traceexec (lua_State *L);
37 |
38 |
39 | #endif
40 |
--------------------------------------------------------------------------------
/lualib/src/main/cpp/lua/ldo.h:
--------------------------------------------------------------------------------
1 | /*
2 | ** $Id: ldo.h,v 2.29 2015/12/21 13:02:14 roberto Exp $
3 | ** Stack and Call structure of Lua
4 | ** See Copyright Notice in lua.h
5 | */
6 |
7 | #ifndef ldo_h
8 | #define ldo_h
9 |
10 |
11 | #include "lobject.h"
12 | #include "lstate.h"
13 | #include "lzio.h"
14 |
15 |
16 | /*
17 | ** Macro to check stack size and grow stack if needed. Parameters
18 | ** 'pre'/'pos' allow the macro to preserve a pointer into the
19 | ** stack across reallocations, doing the work only when needed.
20 | ** 'condmovestack' is used in heavy tests to force a stack reallocation
21 | ** at every check.
22 | */
23 | #define luaD_checkstackaux(L,n,pre,pos) \
24 | if (L->stack_last - L->top <= (n)) \
25 | { pre; luaD_growstack(L, n); pos; } else { condmovestack(L,pre,pos); }
26 |
27 | /* In general, 'pre'/'pos' are empty (nothing to save) */
28 | #define luaD_checkstack(L,n) luaD_checkstackaux(L,n,(void)0,(void)0)
29 |
30 |
31 |
32 | #define savestack(L,p) ((char *)(p) - (char *)L->stack)
33 | #define restorestack(L,n) ((TValue *)((char *)L->stack + (n)))
34 |
35 |
36 | /* type of protected functions, to be ran by 'runprotected' */
37 | typedef void (*Pfunc) (lua_State *L, void *ud);
38 |
39 | LUAI_FUNC int luaD_protectedparser (lua_State *L, ZIO *z, const char *name,
40 | const char *mode);
41 | LUAI_FUNC void luaD_hook (lua_State *L, int event, int line);
42 | LUAI_FUNC int luaD_precall (lua_State *L, StkId func, int nresults);
43 | LUAI_FUNC void luaD_call (lua_State *L, StkId func, int nResults);
44 | LUAI_FUNC void luaD_callnoyield (lua_State *L, StkId func, int nResults);
45 | LUAI_FUNC int luaD_pcall (lua_State *L, Pfunc func, void *u,
46 | ptrdiff_t oldtop, ptrdiff_t ef);
47 | LUAI_FUNC int luaD_poscall (lua_State *L, CallInfo *ci, StkId firstResult,
48 | int nres);
49 | LUAI_FUNC void luaD_reallocstack (lua_State *L, int newsize);
50 | LUAI_FUNC void luaD_growstack (lua_State *L, int n);
51 | LUAI_FUNC void luaD_shrinkstack (lua_State *L);
52 | LUAI_FUNC void luaD_inctop (lua_State *L);
53 |
54 | LUAI_FUNC l_noret luaD_throw (lua_State *L, int errcode);
55 | LUAI_FUNC int luaD_rawrunprotected (lua_State *L, Pfunc f, void *ud);
56 |
57 | #endif
58 |
59 |
--------------------------------------------------------------------------------
/lualib/src/main/cpp/lua/ldump.c:
--------------------------------------------------------------------------------
1 | /*
2 | ** $Id: ldump.c,v 2.37 2015/10/08 15:53:49 roberto Exp $
3 | ** save precompiled Lua chunks
4 | ** See Copyright Notice in lua.h
5 | */
6 |
7 | #define ldump_c
8 | #define LUA_CORE
9 |
10 | #include "lprefix.h"
11 |
12 |
13 | #include
14 |
15 | #include "lua.h"
16 |
17 | #include "lobject.h"
18 | #include "lstate.h"
19 | #include "lundump.h"
20 |
21 |
22 | typedef struct {
23 | lua_State *L;
24 | lua_Writer writer;
25 | void *data;
26 | int strip;
27 | int status;
28 | } DumpState;
29 |
30 |
31 | /*
32 | ** All high-level dumps go through DumpVector; you can change it to
33 | ** change the endianness of the result
34 | */
35 | #define DumpVector(v,n,D) DumpBlock(v,(n)*sizeof((v)[0]),D)
36 |
37 | #define DumpLiteral(s,D) DumpBlock(s, sizeof(s) - sizeof(char), D)
38 |
39 |
40 | static void DumpBlock (const void *b, size_t size, DumpState *D) {
41 | if (D->status == 0 && size > 0) {
42 | lua_unlock(D->L);
43 | D->status = (*D->writer)(D->L, b, size, D->data);
44 | lua_lock(D->L);
45 | }
46 | }
47 |
48 |
49 | #define DumpVar(x,D) DumpVector(&x,1,D)
50 |
51 |
52 | static void DumpByte (int y, DumpState *D) {
53 | lu_byte x = (lu_byte)y;
54 | DumpVar(x, D);
55 | }
56 |
57 |
58 | static void DumpInt (int x, DumpState *D) {
59 | DumpVar(x, D);
60 | }
61 |
62 |
63 | static void DumpNumber (lua_Number x, DumpState *D) {
64 | DumpVar(x, D);
65 | }
66 |
67 |
68 | static void DumpInteger (lua_Integer x, DumpState *D) {
69 | DumpVar(x, D);
70 | }
71 |
72 |
73 | static void DumpString (const TString *s, DumpState *D) {
74 | if (s == NULL)
75 | DumpByte(0, D);
76 | else {
77 | size_t size = tsslen(s) + 1; /* include trailing '\0' */
78 | const char *str = getstr(s);
79 | if (size < 0xFF)
80 | DumpByte(cast_int(size), D);
81 | else {
82 | DumpByte(0xFF, D);
83 | DumpVar(size, D);
84 | }
85 | DumpVector(str, size - 1, D); /* no need to save '\0' */
86 | }
87 | }
88 |
89 |
90 | static void DumpCode (const Proto *f, DumpState *D) {
91 | DumpInt(f->sizecode, D);
92 | DumpVector(f->code, f->sizecode, D);
93 | }
94 |
95 |
96 | static void DumpFunction(const Proto *f, TString *psource, DumpState *D);
97 |
98 | static void DumpConstants (const Proto *f, DumpState *D) {
99 | int i;
100 | int n = f->sizek;
101 | DumpInt(n, D);
102 | for (i = 0; i < n; i++) {
103 | const TValue *o = &f->k[i];
104 | DumpByte(ttype(o), D);
105 | switch (ttype(o)) {
106 | case LUA_TNIL:
107 | break;
108 | case LUA_TBOOLEAN:
109 | DumpByte(bvalue(o), D);
110 | break;
111 | case LUA_TNUMFLT:
112 | DumpNumber(fltvalue(o), D);
113 | break;
114 | case LUA_TNUMINT:
115 | DumpInteger(ivalue(o), D);
116 | break;
117 | case LUA_TSHRSTR:
118 | case LUA_TLNGSTR:
119 | DumpString(tsvalue(o), D);
120 | break;
121 | default:
122 | lua_assert(0);
123 | }
124 | }
125 | }
126 |
127 |
128 | static void DumpProtos (const Proto *f, DumpState *D) {
129 | int i;
130 | int n = f->sizep;
131 | DumpInt(n, D);
132 | for (i = 0; i < n; i++)
133 | DumpFunction(f->p[i], f->source, D);
134 | }
135 |
136 |
137 | static void DumpUpvalues (const Proto *f, DumpState *D) {
138 | int i, n = f->sizeupvalues;
139 | DumpInt(n, D);
140 | for (i = 0; i < n; i++) {
141 | DumpByte(f->upvalues[i].instack, D);
142 | DumpByte(f->upvalues[i].idx, D);
143 | }
144 | }
145 |
146 |
147 | static void DumpDebug (const Proto *f, DumpState *D) {
148 | int i, n;
149 | n = (D->strip) ? 0 : f->sizelineinfo;
150 | DumpInt(n, D);
151 | DumpVector(f->lineinfo, n, D);
152 | n = (D->strip) ? 0 : f->sizelocvars;
153 | DumpInt(n, D);
154 | for (i = 0; i < n; i++) {
155 | DumpString(f->locvars[i].varname, D);
156 | DumpInt(f->locvars[i].startpc, D);
157 | DumpInt(f->locvars[i].endpc, D);
158 | }
159 | n = (D->strip) ? 0 : f->sizeupvalues;
160 | DumpInt(n, D);
161 | for (i = 0; i < n; i++)
162 | DumpString(f->upvalues[i].name, D);
163 | }
164 |
165 |
166 | static void DumpFunction (const Proto *f, TString *psource, DumpState *D) {
167 | if (D->strip || f->source == psource)
168 | DumpString(NULL, D); /* no debug info or same source as its parent */
169 | else
170 | DumpString(f->source, D);
171 | DumpInt(f->linedefined, D);
172 | DumpInt(f->lastlinedefined, D);
173 | DumpByte(f->numparams, D);
174 | DumpByte(f->is_vararg, D);
175 | DumpByte(f->maxstacksize, D);
176 | DumpCode(f, D);
177 | DumpConstants(f, D);
178 | DumpUpvalues(f, D);
179 | DumpProtos(f, D);
180 | DumpDebug(f, D);
181 | }
182 |
183 |
184 | static void DumpHeader (DumpState *D) {
185 | DumpLiteral(LUA_SIGNATURE, D);
186 | DumpByte(LUAC_VERSION, D);
187 | DumpByte(LUAC_FORMAT, D);
188 | DumpLiteral(LUAC_DATA, D);
189 | DumpByte(sizeof(int), D);
190 | DumpByte(sizeof(size_t), D);
191 | DumpByte(sizeof(Instruction), D);
192 | DumpByte(sizeof(lua_Integer), D);
193 | DumpByte(sizeof(lua_Number), D);
194 | DumpInteger(LUAC_INT, D);
195 | DumpNumber(LUAC_NUM, D);
196 | }
197 |
198 |
199 | /*
200 | ** dump Lua function as precompiled chunk
201 | */
202 | int luaU_dump(lua_State *L, const Proto *f, lua_Writer w, void *data,
203 | int strip) {
204 | DumpState D;
205 | D.L = L;
206 | D.writer = w;
207 | D.data = data;
208 | D.strip = strip;
209 | D.status = 0;
210 | DumpHeader(&D);
211 | DumpByte(f->sizeupvalues, &D);
212 | DumpFunction(f, NULL, &D);
213 | return D.status;
214 | }
215 |
216 |
--------------------------------------------------------------------------------
/lualib/src/main/cpp/lua/lfunc.c:
--------------------------------------------------------------------------------
1 | /*
2 | ** $Id: lfunc.c,v 2.45 2014/11/02 19:19:04 roberto Exp $
3 | ** Auxiliary functions to manipulate prototypes and closures
4 | ** See Copyright Notice in lua.h
5 | */
6 |
7 | #define lfunc_c
8 | #define LUA_CORE
9 |
10 | #include "lprefix.h"
11 |
12 |
13 | #include
14 |
15 | #include "lua.h"
16 |
17 | #include "lfunc.h"
18 | #include "lgc.h"
19 | #include "lmem.h"
20 | #include "lobject.h"
21 | #include "lstate.h"
22 |
23 |
24 |
25 | CClosure *luaF_newCclosure (lua_State *L, int n) {
26 | GCObject *o = luaC_newobj(L, LUA_TCCL, sizeCclosure(n));
27 | CClosure *c = gco2ccl(o);
28 | c->nupvalues = cast_byte(n);
29 | return c;
30 | }
31 |
32 |
33 | LClosure *luaF_newLclosure (lua_State *L, int n) {
34 | GCObject *o = luaC_newobj(L, LUA_TLCL, sizeLclosure(n));
35 | LClosure *c = gco2lcl(o);
36 | c->p = NULL;
37 | c->nupvalues = cast_byte(n);
38 | while (n--) c->upvals[n] = NULL;
39 | return c;
40 | }
41 |
42 | /*
43 | ** fill a closure with new closed upvalues
44 | */
45 | void luaF_initupvals (lua_State *L, LClosure *cl) {
46 | int i;
47 | for (i = 0; i < cl->nupvalues; i++) {
48 | UpVal *uv = luaM_new(L, UpVal);
49 | uv->refcount = 1;
50 | uv->v = &uv->u.value; /* make it closed */
51 | setnilvalue(uv->v);
52 | cl->upvals[i] = uv;
53 | }
54 | }
55 |
56 |
57 | UpVal *luaF_findupval (lua_State *L, StkId level) {
58 | UpVal **pp = &L->openupval;
59 | UpVal *p;
60 | UpVal *uv;
61 | lua_assert(isintwups(L) || L->openupval == NULL);
62 | while (*pp != NULL && (p = *pp)->v >= level) {
63 | lua_assert(upisopen(p));
64 | if (p->v == level) /* found a corresponding upvalue? */
65 | return p; /* return it */
66 | pp = &p->u.open.next;
67 | }
68 | /* not found: create a new upvalue */
69 | uv = luaM_new(L, UpVal);
70 | uv->refcount = 0;
71 | uv->u.open.next = *pp; /* link it to list of open upvalues */
72 | uv->u.open.touched = 1;
73 | *pp = uv;
74 | uv->v = level; /* current value lives in the stack */
75 | if (!isintwups(L)) { /* thread not in list of threads with upvalues? */
76 | L->twups = G(L)->twups; /* link it to the list */
77 | G(L)->twups = L;
78 | }
79 | return uv;
80 | }
81 |
82 |
83 | void luaF_close (lua_State *L, StkId level) {
84 | UpVal *uv;
85 | while (L->openupval != NULL && (uv = L->openupval)->v >= level) {
86 | lua_assert(upisopen(uv));
87 | L->openupval = uv->u.open.next; /* remove from 'open' list */
88 | if (uv->refcount == 0) /* no references? */
89 | luaM_free(L, uv); /* free upvalue */
90 | else {
91 | setobj(L, &uv->u.value, uv->v); /* move value to upvalue slot */
92 | uv->v = &uv->u.value; /* now current value lives here */
93 | luaC_upvalbarrier(L, uv);
94 | }
95 | }
96 | }
97 |
98 |
99 | Proto *luaF_newproto (lua_State *L) {
100 | GCObject *o = luaC_newobj(L, LUA_TPROTO, sizeof(Proto));
101 | Proto *f = gco2p(o);
102 | f->k = NULL;
103 | f->sizek = 0;
104 | f->p = NULL;
105 | f->sizep = 0;
106 | f->code = NULL;
107 | f->cache = NULL;
108 | f->sizecode = 0;
109 | f->lineinfo = NULL;
110 | f->sizelineinfo = 0;
111 | f->upvalues = NULL;
112 | f->sizeupvalues = 0;
113 | f->numparams = 0;
114 | f->is_vararg = 0;
115 | f->maxstacksize = 0;
116 | f->locvars = NULL;
117 | f->sizelocvars = 0;
118 | f->linedefined = 0;
119 | f->lastlinedefined = 0;
120 | f->source = NULL;
121 | return f;
122 | }
123 |
124 |
125 | void luaF_freeproto (lua_State *L, Proto *f) {
126 | luaM_freearray(L, f->code, f->sizecode);
127 | luaM_freearray(L, f->p, f->sizep);
128 | luaM_freearray(L, f->k, f->sizek);
129 | luaM_freearray(L, f->lineinfo, f->sizelineinfo);
130 | luaM_freearray(L, f->locvars, f->sizelocvars);
131 | luaM_freearray(L, f->upvalues, f->sizeupvalues);
132 | luaM_free(L, f);
133 | }
134 |
135 |
136 | /*
137 | ** Look for n-th local variable at line 'line' in function 'func'.
138 | ** Returns NULL if not found.
139 | */
140 | const char *luaF_getlocalname (const Proto *f, int local_number, int pc) {
141 | int i;
142 | for (i = 0; isizelocvars && f->locvars[i].startpc <= pc; i++) {
143 | if (pc < f->locvars[i].endpc) { /* is variable active? */
144 | local_number--;
145 | if (local_number == 0)
146 | return getstr(f->locvars[i].varname);
147 | }
148 | }
149 | return NULL; /* not found */
150 | }
151 |
152 |
--------------------------------------------------------------------------------
/lualib/src/main/cpp/lua/lfunc.h:
--------------------------------------------------------------------------------
1 | /*
2 | ** $Id: lfunc.h,v 2.15 2015/01/13 15:49:11 roberto Exp $
3 | ** Auxiliary functions to manipulate prototypes and closures
4 | ** See Copyright Notice in lua.h
5 | */
6 |
7 | #ifndef lfunc_h
8 | #define lfunc_h
9 |
10 |
11 | #include "lobject.h"
12 |
13 |
14 | #define sizeCclosure(n) (cast(int, sizeof(CClosure)) + \
15 | cast(int, sizeof(TValue)*((n)-1)))
16 |
17 | #define sizeLclosure(n) (cast(int, sizeof(LClosure)) + \
18 | cast(int, sizeof(TValue *)*((n)-1)))
19 |
20 |
21 | /* test whether thread is in 'twups' list */
22 | #define isintwups(L) (L->twups != L)
23 |
24 |
25 | /*
26 | ** maximum number of upvalues in a closure (both C and Lua). (Value
27 | ** must fit in a VM register.)
28 | */
29 | #define MAXUPVAL 255
30 |
31 |
32 | /*
33 | ** Upvalues for Lua closures
34 | */
35 | struct UpVal {
36 | TValue *v; /* points to stack or to its own value */
37 | lu_mem refcount; /* reference counter */
38 | union {
39 | struct { /* (when open) */
40 | UpVal *next; /* linked list */
41 | int touched; /* mark to avoid cycles with dead threads */
42 | } open;
43 | TValue value; /* the value (when closed) */
44 | } u;
45 | };
46 |
47 | #define upisopen(up) ((up)->v != &(up)->u.value)
48 |
49 |
50 | LUAI_FUNC Proto *luaF_newproto (lua_State *L);
51 | LUAI_FUNC CClosure *luaF_newCclosure (lua_State *L, int nelems);
52 | LUAI_FUNC LClosure *luaF_newLclosure (lua_State *L, int nelems);
53 | LUAI_FUNC void luaF_initupvals (lua_State *L, LClosure *cl);
54 | LUAI_FUNC UpVal *luaF_findupval (lua_State *L, StkId level);
55 | LUAI_FUNC void luaF_close (lua_State *L, StkId level);
56 | LUAI_FUNC void luaF_freeproto (lua_State *L, Proto *f);
57 | LUAI_FUNC const char *luaF_getlocalname (const Proto *func, int local_number,
58 | int pc);
59 |
60 |
61 | #endif
62 |
--------------------------------------------------------------------------------
/lualib/src/main/cpp/lua/lgc.h:
--------------------------------------------------------------------------------
1 | /*
2 | ** $Id: lgc.h,v 2.91 2015/12/21 13:02:14 roberto Exp $
3 | ** Garbage Collector
4 | ** See Copyright Notice in lua.h
5 | */
6 |
7 | #ifndef lgc_h
8 | #define lgc_h
9 |
10 |
11 | #include "lobject.h"
12 | #include "lstate.h"
13 |
14 | /*
15 | ** Collectable objects may have one of three colors: white, which
16 | ** means the object is not marked; gray, which means the
17 | ** object is marked, but its references may be not marked; and
18 | ** black, which means that the object and all its references are marked.
19 | ** The main invariant of the garbage collector, while marking objects,
20 | ** is that a black object can never point to a white one. Moreover,
21 | ** any gray object must be in a "gray list" (gray, grayagain, weak,
22 | ** allweak, ephemeron) so that it can be visited again before finishing
23 | ** the collection cycle. These lists have no meaning when the invariant
24 | ** is not being enforced (e.g., sweep phase).
25 | */
26 |
27 |
28 |
29 | /* how much to allocate before next GC step */
30 | #if !defined(GCSTEPSIZE)
31 | /* ~100 small strings */
32 | #define GCSTEPSIZE (cast_int(100 * sizeof(TString)))
33 | #endif
34 |
35 |
36 | /*
37 | ** Possible states of the Garbage Collector
38 | */
39 | #define GCSpropagate 0
40 | #define GCSatomic 1
41 | #define GCSswpallgc 2
42 | #define GCSswpfinobj 3
43 | #define GCSswptobefnz 4
44 | #define GCSswpend 5
45 | #define GCScallfin 6
46 | #define GCSpause 7
47 |
48 |
49 | #define issweepphase(g) \
50 | (GCSswpallgc <= (g)->gcstate && (g)->gcstate <= GCSswpend)
51 |
52 |
53 | /*
54 | ** macro to tell when main invariant (white objects cannot point to black
55 | ** ones) must be kept. During a collection, the sweep
56 | ** phase may break the invariant, as objects turned white may point to
57 | ** still-black objects. The invariant is restored when sweep ends and
58 | ** all objects are white again.
59 | */
60 |
61 | #define keepinvariant(g) ((g)->gcstate <= GCSatomic)
62 |
63 |
64 | /*
65 | ** some useful bit tricks
66 | */
67 | #define resetbits(x,m) ((x) &= cast(lu_byte, ~(m)))
68 | #define setbits(x,m) ((x) |= (m))
69 | #define testbits(x,m) ((x) & (m))
70 | #define bitmask(b) (1<<(b))
71 | #define bit2mask(b1,b2) (bitmask(b1) | bitmask(b2))
72 | #define l_setbit(x,b) setbits(x, bitmask(b))
73 | #define resetbit(x,b) resetbits(x, bitmask(b))
74 | #define testbit(x,b) testbits(x, bitmask(b))
75 |
76 |
77 | /* Layout for bit use in 'marked' field: */
78 | #define WHITE0BIT 0 /* object is white (type 0) */
79 | #define WHITE1BIT 1 /* object is white (type 1) */
80 | #define BLACKBIT 2 /* object is black */
81 | #define FINALIZEDBIT 3 /* object has been marked for finalization */
82 | /* bit 7 is currently used by tests (luaL_checkmemory) */
83 |
84 | #define WHITEBITS bit2mask(WHITE0BIT, WHITE1BIT)
85 |
86 |
87 | #define iswhite(x) testbits((x)->marked, WHITEBITS)
88 | #define isblack(x) testbit((x)->marked, BLACKBIT)
89 | #define isgray(x) /* neither white nor black */ \
90 | (!testbits((x)->marked, WHITEBITS | bitmask(BLACKBIT)))
91 |
92 | #define tofinalize(x) testbit((x)->marked, FINALIZEDBIT)
93 |
94 | #define otherwhite(g) ((g)->currentwhite ^ WHITEBITS)
95 | #define isdeadm(ow,m) (!(((m) ^ WHITEBITS) & (ow)))
96 | #define isdead(g,v) isdeadm(otherwhite(g), (v)->marked)
97 |
98 | #define changewhite(x) ((x)->marked ^= WHITEBITS)
99 | #define gray2black(x) l_setbit((x)->marked, BLACKBIT)
100 |
101 | #define luaC_white(g) cast(lu_byte, (g)->currentwhite & WHITEBITS)
102 |
103 |
104 | /*
105 | ** Does one step of collection when debt becomes positive. 'pre'/'pos'
106 | ** allows some adjustments to be done only when needed. macro
107 | ** 'condchangemem' is used only for heavy tests (forcing a full
108 | ** GC cycle on every opportunity)
109 | */
110 | #define luaC_condGC(L,pre,pos) \
111 | { if (G(L)->GCdebt > 0) { pre; luaC_step(L); pos;}; \
112 | condchangemem(L,pre,pos); }
113 |
114 | /* more often than not, 'pre'/'pos' are empty */
115 | #define luaC_checkGC(L) luaC_condGC(L,(void)0,(void)0)
116 |
117 |
118 | #define luaC_barrier(L,p,v) ( \
119 | (iscollectable(v) && isblack(p) && iswhite(gcvalue(v))) ? \
120 | luaC_barrier_(L,obj2gco(p),gcvalue(v)) : cast_void(0))
121 |
122 | #define luaC_barrierback(L,p,v) ( \
123 | (iscollectable(v) && isblack(p) && iswhite(gcvalue(v))) ? \
124 | luaC_barrierback_(L,p) : cast_void(0))
125 |
126 | #define luaC_objbarrier(L,p,o) ( \
127 | (isblack(p) && iswhite(o)) ? \
128 | luaC_barrier_(L,obj2gco(p),obj2gco(o)) : cast_void(0))
129 |
130 | #define luaC_upvalbarrier(L,uv) ( \
131 | (iscollectable((uv)->v) && !upisopen(uv)) ? \
132 | luaC_upvalbarrier_(L,uv) : cast_void(0))
133 |
134 | LUAI_FUNC void luaC_fix (lua_State *L, GCObject *o);
135 | LUAI_FUNC void luaC_freeallobjects (lua_State *L);
136 | LUAI_FUNC void luaC_step (lua_State *L);
137 | LUAI_FUNC void luaC_runtilstate (lua_State *L, int statesmask);
138 | LUAI_FUNC void luaC_fullgc (lua_State *L, int isemergency);
139 | LUAI_FUNC GCObject *luaC_newobj (lua_State *L, int tt, size_t sz);
140 | LUAI_FUNC void luaC_barrier_ (lua_State *L, GCObject *o, GCObject *v);
141 | LUAI_FUNC void luaC_barrierback_ (lua_State *L, Table *o);
142 | LUAI_FUNC void luaC_upvalbarrier_ (lua_State *L, UpVal *uv);
143 | LUAI_FUNC void luaC_checkfinalizer (lua_State *L, GCObject *o, Table *mt);
144 | LUAI_FUNC void luaC_upvdeccount (lua_State *L, UpVal *uv);
145 |
146 |
147 | #endif
148 |
--------------------------------------------------------------------------------
/lualib/src/main/cpp/lua/linit.c:
--------------------------------------------------------------------------------
1 | /*
2 | ** $Id: linit.c,v 1.39 2016/12/04 20:17:24 roberto Exp $
3 | ** Initialization of libraries for lua.c and other clients
4 | ** See Copyright Notice in lua.h
5 | */
6 |
7 |
8 | #define linit_c
9 | #define LUA_LIB
10 |
11 | /*
12 | ** If you embed Lua in your program and need to open the standard
13 | ** libraries, call luaL_openlibs in your program. If you need a
14 | ** different set of libraries, copy this file to your project and edit
15 | ** it to suit your needs.
16 | **
17 | ** You can also *preload* libraries, so that a later 'require' can
18 | ** open the library, which is already linked to the application.
19 | ** For that, do the following code:
20 | **
21 | ** luaL_getsubtable(L, LUA_REGISTRYINDEX, LUA_PRELOAD_TABLE);
22 | ** lua_pushcfunction(L, luaopen_modname);
23 | ** lua_setfield(L, -2, modname);
24 | ** lua_pop(L, 1); // remove PRELOAD table
25 | */
26 |
27 | #include "lprefix.h"
28 |
29 |
30 | #include
31 |
32 | #include "lua.h"
33 |
34 | #include "lualib.h"
35 | #include "lauxlib.h"
36 |
37 |
38 | /*
39 | ** these libs are loaded by lua.c and are readily available to any Lua
40 | ** program
41 | */
42 | static const luaL_Reg loadedlibs[] = {
43 | {"_G", luaopen_base},
44 | {LUA_LOADLIBNAME, luaopen_package},
45 | {LUA_COLIBNAME, luaopen_coroutine},
46 | {LUA_TABLIBNAME, luaopen_table},
47 | {LUA_IOLIBNAME, luaopen_io},
48 | {LUA_OSLIBNAME, luaopen_os},
49 | {LUA_STRLIBNAME, luaopen_string},
50 | {LUA_MATHLIBNAME, luaopen_math},
51 | {LUA_UTF8LIBNAME, luaopen_utf8},
52 | {LUA_DBLIBNAME, luaopen_debug},
53 | #if defined(LUA_COMPAT_BITLIB)
54 | {LUA_BITLIBNAME, luaopen_bit32},
55 | #endif
56 | {NULL, NULL}
57 | };
58 |
59 |
60 | LUALIB_API void luaL_openlibs (lua_State *L) {
61 | const luaL_Reg *lib;
62 | /* "require" functions from 'loadedlibs' and set results to global table */
63 | for (lib = loadedlibs; lib->func; lib++) {
64 | luaL_requiref(L, lib->name, lib->func, 1);
65 | lua_pop(L, 1); /* remove lib */
66 | }
67 | }
68 |
69 |
--------------------------------------------------------------------------------
/lualib/src/main/cpp/lua/llex.h:
--------------------------------------------------------------------------------
1 | /*
2 | ** $Id: llex.h,v 1.79 2016/05/02 14:02:12 roberto Exp $
3 | ** Lexical Analyzer
4 | ** See Copyright Notice in lua.h
5 | */
6 |
7 | #ifndef llex_h
8 | #define llex_h
9 |
10 | #include "lobject.h"
11 | #include "lzio.h"
12 |
13 |
14 | #define FIRST_RESERVED 257
15 |
16 |
17 | #if !defined(LUA_ENV)
18 | #define LUA_ENV "_ENV"
19 | #endif
20 |
21 |
22 | /*
23 | * WARNING: if you change the order of this enumeration,
24 | * grep "ORDER RESERVED"
25 | */
26 | enum RESERVED {
27 | /* terminal symbols denoted by reserved words */
28 | TK_AND = FIRST_RESERVED, TK_BREAK,
29 | TK_DO, TK_ELSE, TK_ELSEIF, TK_END, TK_FALSE, TK_FOR, TK_FUNCTION,
30 | TK_GOTO, TK_IF, TK_IN, TK_LOCAL, TK_NIL, TK_NOT, TK_OR, TK_REPEAT,
31 | TK_RETURN, TK_THEN, TK_TRUE, TK_UNTIL, TK_WHILE,
32 | /* other terminal symbols */
33 | TK_IDIV, TK_CONCAT, TK_DOTS, TK_EQ, TK_GE, TK_LE, TK_NE,
34 | TK_SHL, TK_SHR,
35 | TK_DBCOLON, TK_EOS,
36 | TK_FLT, TK_INT, TK_NAME, TK_STRING
37 | };
38 |
39 | /* number of reserved words */
40 | #define NUM_RESERVED (cast(int, TK_WHILE-FIRST_RESERVED+1))
41 |
42 |
43 | typedef union {
44 | lua_Number r;
45 | lua_Integer i;
46 | TString *ts;
47 | } SemInfo; /* semantics information */
48 |
49 |
50 | typedef struct Token {
51 | int token;
52 | SemInfo seminfo;
53 | } Token;
54 |
55 |
56 | /* state of the lexer plus state of the parser when shared by all
57 | functions */
58 | typedef struct LexState {
59 | int current; /* current character (charint) */
60 | int linenumber; /* input line counter */
61 | int lastline; /* line of last token 'consumed' */
62 | Token t; /* current token */
63 | Token lookahead; /* look ahead token */
64 | struct FuncState *fs; /* current function (parser) */
65 | struct lua_State *L;
66 | ZIO *z; /* input stream */
67 | Mbuffer *buff; /* buffer for tokens */
68 | Table *h; /* to avoid collection/reuse strings */
69 | struct Dyndata *dyd; /* dynamic structures used by the parser */
70 | TString *source; /* current source name */
71 | TString *envn; /* environment variable name */
72 | } LexState;
73 |
74 |
75 | LUAI_FUNC void luaX_init (lua_State *L);
76 | LUAI_FUNC void luaX_setinput (lua_State *L, LexState *ls, ZIO *z,
77 | TString *source, int firstchar);
78 | LUAI_FUNC TString *luaX_newstring (LexState *ls, const char *str, size_t l);
79 | LUAI_FUNC void luaX_next (LexState *ls);
80 | LUAI_FUNC int luaX_lookahead (LexState *ls);
81 | LUAI_FUNC l_noret luaX_syntaxerror (LexState *ls, const char *s);
82 | LUAI_FUNC const char *luaX_token2str (LexState *ls, int token);
83 |
84 |
85 | #endif
86 |
--------------------------------------------------------------------------------
/lualib/src/main/cpp/lua/llimits.h:
--------------------------------------------------------------------------------
1 | /*
2 | ** $Id: llimits.h,v 1.141 2015/11/19 19:16:22 roberto Exp $
3 | ** Limits, basic types, and some other 'installation-dependent' definitions
4 | ** See Copyright Notice in lua.h
5 | */
6 |
7 | #ifndef llimits_h
8 | #define llimits_h
9 |
10 |
11 | #include
12 | #include
13 |
14 |
15 | #include "lua.h"
16 |
17 | /*
18 | ** 'lu_mem' and 'l_mem' are unsigned/signed integers big enough to count
19 | ** the total memory used by Lua (in bytes). Usually, 'size_t' and
20 | ** 'ptrdiff_t' should work, but we use 'long' for 16-bit machines.
21 | */
22 | #if defined(LUAI_MEM) /* { external definitions? */
23 | typedef LUAI_UMEM lu_mem;
24 | typedef LUAI_MEM l_mem;
25 | #elif LUAI_BITSINT >= 32 /* }{ */
26 | typedef size_t lu_mem;
27 | typedef ptrdiff_t l_mem;
28 | #else /* 16-bit ints */ /* }{ */
29 | typedef unsigned long lu_mem;
30 | typedef long l_mem;
31 | #endif /* } */
32 |
33 |
34 | /* chars used as small naturals (so that 'char' is reserved for characters) */
35 | typedef unsigned char lu_byte;
36 |
37 |
38 | /* maximum value for size_t */
39 | #define MAX_SIZET ((size_t)(~(size_t)0))
40 |
41 | /* maximum size visible for Lua (must be representable in a lua_Integer */
42 | #define MAX_SIZE (sizeof(size_t) < sizeof(lua_Integer) ? MAX_SIZET \
43 | : (size_t)(LUA_MAXINTEGER))
44 |
45 |
46 | #define MAX_LUMEM ((lu_mem)(~(lu_mem)0))
47 |
48 | #define MAX_LMEM ((l_mem)(MAX_LUMEM >> 1))
49 |
50 |
51 | #define MAX_INT INT_MAX /* maximum value of an int */
52 |
53 |
54 | /*
55 | ** conversion of pointer to unsigned integer:
56 | ** this is for hashing only; there is no problem if the integer
57 | ** cannot hold the whole pointer value
58 | */
59 | #define point2uint(p) ((unsigned int)((size_t)(p) & UINT_MAX))
60 |
61 |
62 |
63 | /* type to ensure maximum alignment */
64 | #if defined(LUAI_USER_ALIGNMENT_T)
65 | typedef LUAI_USER_ALIGNMENT_T L_Umaxalign;
66 | #else
67 | typedef union {
68 | lua_Number n;
69 | double u;
70 | void *s;
71 | lua_Integer i;
72 | long l;
73 | } L_Umaxalign;
74 | #endif
75 |
76 |
77 |
78 | /* types of 'usual argument conversions' for lua_Number and lua_Integer */
79 | typedef LUAI_UACNUMBER l_uacNumber;
80 | typedef LUAI_UACINT l_uacInt;
81 |
82 |
83 | /* internal assertions for in-house debugging */
84 | #if defined(lua_assert)
85 | #define check_exp(c,e) (lua_assert(c), (e))
86 | /* to avoid problems with conditions too long */
87 | #define lua_longassert(c) ((c) ? (void)0 : lua_assert(0))
88 | #else
89 | #define lua_assert(c) ((void)0)
90 | #define check_exp(c,e) (e)
91 | #define lua_longassert(c) ((void)0)
92 | #endif
93 |
94 | /*
95 | ** assertion for checking API calls
96 | */
97 | #if !defined(luai_apicheck)
98 | #define luai_apicheck(l,e) lua_assert(e)
99 | #endif
100 |
101 | #define api_check(l,e,msg) luai_apicheck(l,(e) && msg)
102 |
103 |
104 | /* macro to avoid warnings about unused variables */
105 | #if !defined(UNUSED)
106 | #define UNUSED(x) ((void)(x))
107 | #endif
108 |
109 |
110 | /* type casts (a macro highlights casts in the code) */
111 | #define cast(t, exp) ((t)(exp))
112 |
113 | #define cast_void(i) cast(void, (i))
114 | #define cast_byte(i) cast(lu_byte, (i))
115 | #define cast_num(i) cast(lua_Number, (i))
116 | #define cast_int(i) cast(int, (i))
117 | #define cast_uchar(i) cast(unsigned char, (i))
118 |
119 |
120 | /* cast a signed lua_Integer to lua_Unsigned */
121 | #if !defined(l_castS2U)
122 | #define l_castS2U(i) ((lua_Unsigned)(i))
123 | #endif
124 |
125 | /*
126 | ** cast a lua_Unsigned to a signed lua_Integer; this cast is
127 | ** not strict ISO C, but two-complement architectures should
128 | ** work fine.
129 | */
130 | #if !defined(l_castU2S)
131 | #define l_castU2S(i) ((lua_Integer)(i))
132 | #endif
133 |
134 |
135 | /*
136 | ** non-return type
137 | */
138 | #if defined(__GNUC__)
139 | #define l_noret void __attribute__((noreturn))
140 | #elif defined(_MSC_VER) && _MSC_VER >= 1200
141 | #define l_noret void __declspec(noreturn)
142 | #else
143 | #define l_noret void
144 | #endif
145 |
146 |
147 |
148 | /*
149 | ** maximum depth for nested C calls and syntactical nested non-terminals
150 | ** in a program. (Value must fit in an unsigned short int.)
151 | */
152 | #if !defined(LUAI_MAXCCALLS)
153 | #define LUAI_MAXCCALLS 200
154 | #endif
155 |
156 |
157 |
158 | /*
159 | ** type for virtual-machine instructions;
160 | ** must be an unsigned with (at least) 4 bytes (see details in lopcodes.h)
161 | */
162 | #if LUAI_BITSINT >= 32
163 | typedef unsigned int Instruction;
164 | #else
165 | typedef unsigned long Instruction;
166 | #endif
167 |
168 |
169 |
170 | /*
171 | ** Maximum length for short strings, that is, strings that are
172 | ** internalized. (Cannot be smaller than reserved words or tags for
173 | ** metamethods, as these strings must be internalized;
174 | ** #("function") = 8, #("__newindex") = 10.)
175 | */
176 | #if !defined(LUAI_MAXSHORTLEN)
177 | #define LUAI_MAXSHORTLEN 40
178 | #endif
179 |
180 |
181 | /*
182 | ** Initial size for the string table (must be power of 2).
183 | ** The Lua core alone registers ~50 strings (reserved words +
184 | ** metaevent keys + a few others). Libraries would typically add
185 | ** a few dozens more.
186 | */
187 | #if !defined(MINSTRTABSIZE)
188 | #define MINSTRTABSIZE 128
189 | #endif
190 |
191 |
192 | /*
193 | ** Size of cache for strings in the API. 'N' is the number of
194 | ** sets (better be a prime) and "M" is the size of each set (M == 1
195 | ** makes a direct cache.)
196 | */
197 | #if !defined(STRCACHE_N)
198 | #define STRCACHE_N 53
199 | #define STRCACHE_M 2
200 | #endif
201 |
202 |
203 | /* minimum size for string buffer */
204 | #if !defined(LUA_MINBUFFER)
205 | #define LUA_MINBUFFER 32
206 | #endif
207 |
208 |
209 | /*
210 | ** macros that are executed whenever program enters the Lua core
211 | ** ('lua_lock') and leaves the core ('lua_unlock')
212 | */
213 | #if !defined(lua_lock)
214 | #define lua_lock(L) ((void) 0)
215 | #define lua_unlock(L) ((void) 0)
216 | #endif
217 |
218 | /*
219 | ** macro executed during Lua functions at points where the
220 | ** function can yield.
221 | */
222 | #if !defined(luai_threadyield)
223 | #define luai_threadyield(L) {lua_unlock(L); lua_lock(L);}
224 | #endif
225 |
226 |
227 | /*
228 | ** these macros allow user-specific actions on threads when you defined
229 | ** LUAI_EXTRASPACE and need to do something extra when a thread is
230 | ** created/deleted/resumed/yielded.
231 | */
232 | #if !defined(luai_userstateopen)
233 | #define luai_userstateopen(L) ((void)L)
234 | #endif
235 |
236 | #if !defined(luai_userstateclose)
237 | #define luai_userstateclose(L) ((void)L)
238 | #endif
239 |
240 | #if !defined(luai_userstatethread)
241 | #define luai_userstatethread(L,L1) ((void)L)
242 | #endif
243 |
244 | #if !defined(luai_userstatefree)
245 | #define luai_userstatefree(L,L1) ((void)L)
246 | #endif
247 |
248 | #if !defined(luai_userstateresume)
249 | #define luai_userstateresume(L,n) ((void)L)
250 | #endif
251 |
252 | #if !defined(luai_userstateyield)
253 | #define luai_userstateyield(L,n) ((void)L)
254 | #endif
255 |
256 |
257 |
258 | /*
259 | ** The luai_num* macros define the primitive operations over numbers.
260 | */
261 |
262 | /* floor division (defined as 'floor(a/b)') */
263 | #if !defined(luai_numidiv)
264 | #define luai_numidiv(L,a,b) ((void)L, l_floor(luai_numdiv(L,a,b)))
265 | #endif
266 |
267 | /* float division */
268 | #if !defined(luai_numdiv)
269 | #define luai_numdiv(L,a,b) ((a)/(b))
270 | #endif
271 |
272 | /*
273 | ** modulo: defined as 'a - floor(a/b)*b'; this definition gives NaN when
274 | ** 'b' is huge, but the result should be 'a'. 'fmod' gives the result of
275 | ** 'a - trunc(a/b)*b', and therefore must be corrected when 'trunc(a/b)
276 | ** ~= floor(a/b)'. That happens when the division has a non-integer
277 | ** negative result, which is equivalent to the test below.
278 | */
279 | #if !defined(luai_nummod)
280 | #define luai_nummod(L,a,b,m) \
281 | { (m) = l_mathop(fmod)(a,b); if ((m)*(b) < 0) (m) += (b); }
282 | #endif
283 |
284 | /* exponentiation */
285 | #if !defined(luai_numpow)
286 | #define luai_numpow(L,a,b) ((void)L, l_mathop(pow)(a,b))
287 | #endif
288 |
289 | /* the others are quite standard operations */
290 | #if !defined(luai_numadd)
291 | #define luai_numadd(L,a,b) ((a)+(b))
292 | #define luai_numsub(L,a,b) ((a)-(b))
293 | #define luai_nummul(L,a,b) ((a)*(b))
294 | #define luai_numunm(L,a) (-(a))
295 | #define luai_numeq(a,b) ((a)==(b))
296 | #define luai_numlt(a,b) ((a)<(b))
297 | #define luai_numle(a,b) ((a)<=(b))
298 | #define luai_numisnan(a) (!luai_numeq((a), (a)))
299 | #endif
300 |
301 |
302 |
303 |
304 |
305 | /*
306 | ** macro to control inclusion of some hard tests on stack reallocation
307 | */
308 | #if !defined(HARDSTACKTESTS)
309 | #define condmovestack(L,pre,pos) ((void)0)
310 | #else
311 | /* realloc stack keeping its size */
312 | #define condmovestack(L,pre,pos) \
313 | { int sz_ = (L)->stacksize; pre; luaD_reallocstack((L), sz_); pos; }
314 | #endif
315 |
316 | #if !defined(HARDMEMTESTS)
317 | #define condchangemem(L,pre,pos) ((void)0)
318 | #else
319 | #define condchangemem(L,pre,pos) \
320 | { if (G(L)->gcrunning) { pre; luaC_fullgc(L, 0); pos; } }
321 | #endif
322 |
323 | #endif
324 |
--------------------------------------------------------------------------------
/lualib/src/main/cpp/lua/lmathlib.c:
--------------------------------------------------------------------------------
1 | /*
2 | ** $Id: lmathlib.c,v 1.119 2016/12/22 13:08:50 roberto Exp $
3 | ** Standard mathematical library
4 | ** See Copyright Notice in lua.h
5 | */
6 |
7 | #define lmathlib_c
8 | #define LUA_LIB
9 |
10 | #include "lprefix.h"
11 |
12 |
13 | #include
14 | #include
15 |
16 | #include "lua.h"
17 |
18 | #include "lauxlib.h"
19 | #include "lualib.h"
20 |
21 |
22 | #undef PI
23 | #define PI (l_mathop(3.141592653589793238462643383279502884))
24 |
25 |
26 | #if !defined(l_rand) /* { */
27 | #if defined(LUA_USE_POSIX)
28 | #define l_rand() random()
29 | #define l_srand(x) srandom(x)
30 | #define L_RANDMAX 2147483647 /* (2^31 - 1), following POSIX */
31 | #else
32 | #define l_rand() rand()
33 | #define l_srand(x) srand(x)
34 | #define L_RANDMAX RAND_MAX
35 | #endif
36 | #endif /* } */
37 |
38 |
39 | static int math_abs (lua_State *L) {
40 | if (lua_isinteger(L, 1)) {
41 | lua_Integer n = lua_tointeger(L, 1);
42 | if (n < 0) n = (lua_Integer)(0u - (lua_Unsigned)n);
43 | lua_pushinteger(L, n);
44 | }
45 | else
46 | lua_pushnumber(L, l_mathop(fabs)(luaL_checknumber(L, 1)));
47 | return 1;
48 | }
49 |
50 | static int math_sin (lua_State *L) {
51 | lua_pushnumber(L, l_mathop(sin)(luaL_checknumber(L, 1)));
52 | return 1;
53 | }
54 |
55 | static int math_cos (lua_State *L) {
56 | lua_pushnumber(L, l_mathop(cos)(luaL_checknumber(L, 1)));
57 | return 1;
58 | }
59 |
60 | static int math_tan (lua_State *L) {
61 | lua_pushnumber(L, l_mathop(tan)(luaL_checknumber(L, 1)));
62 | return 1;
63 | }
64 |
65 | static int math_asin (lua_State *L) {
66 | lua_pushnumber(L, l_mathop(asin)(luaL_checknumber(L, 1)));
67 | return 1;
68 | }
69 |
70 | static int math_acos (lua_State *L) {
71 | lua_pushnumber(L, l_mathop(acos)(luaL_checknumber(L, 1)));
72 | return 1;
73 | }
74 |
75 | static int math_atan (lua_State *L) {
76 | lua_Number y = luaL_checknumber(L, 1);
77 | lua_Number x = luaL_optnumber(L, 2, 1);
78 | lua_pushnumber(L, l_mathop(atan2)(y, x));
79 | return 1;
80 | }
81 |
82 |
83 | static int math_toint (lua_State *L) {
84 | int valid;
85 | lua_Integer n = lua_tointegerx(L, 1, &valid);
86 | if (valid)
87 | lua_pushinteger(L, n);
88 | else {
89 | luaL_checkany(L, 1);
90 | lua_pushnil(L); /* value is not convertible to integer */
91 | }
92 | return 1;
93 | }
94 |
95 |
96 | static void pushnumint (lua_State *L, lua_Number d) {
97 | lua_Integer n;
98 | if (lua_numbertointeger(d, &n)) /* does 'd' fit in an integer? */
99 | lua_pushinteger(L, n); /* result is integer */
100 | else
101 | lua_pushnumber(L, d); /* result is float */
102 | }
103 |
104 |
105 | static int math_floor (lua_State *L) {
106 | if (lua_isinteger(L, 1))
107 | lua_settop(L, 1); /* integer is its own floor */
108 | else {
109 | lua_Number d = l_mathop(floor)(luaL_checknumber(L, 1));
110 | pushnumint(L, d);
111 | }
112 | return 1;
113 | }
114 |
115 |
116 | static int math_ceil (lua_State *L) {
117 | if (lua_isinteger(L, 1))
118 | lua_settop(L, 1); /* integer is its own ceil */
119 | else {
120 | lua_Number d = l_mathop(ceil)(luaL_checknumber(L, 1));
121 | pushnumint(L, d);
122 | }
123 | return 1;
124 | }
125 |
126 |
127 | static int math_fmod (lua_State *L) {
128 | if (lua_isinteger(L, 1) && lua_isinteger(L, 2)) {
129 | lua_Integer d = lua_tointeger(L, 2);
130 | if ((lua_Unsigned)d + 1u <= 1u) { /* special cases: -1 or 0 */
131 | luaL_argcheck(L, d != 0, 2, "zero");
132 | lua_pushinteger(L, 0); /* avoid overflow with 0x80000... / -1 */
133 | }
134 | else
135 | lua_pushinteger(L, lua_tointeger(L, 1) % d);
136 | }
137 | else
138 | lua_pushnumber(L, l_mathop(fmod)(luaL_checknumber(L, 1),
139 | luaL_checknumber(L, 2)));
140 | return 1;
141 | }
142 |
143 |
144 | /*
145 | ** next function does not use 'modf', avoiding problems with 'double*'
146 | ** (which is not compatible with 'float*') when lua_Number is not
147 | ** 'double'.
148 | */
149 | static int math_modf (lua_State *L) {
150 | if (lua_isinteger(L ,1)) {
151 | lua_settop(L, 1); /* number is its own integer part */
152 | lua_pushnumber(L, 0); /* no fractional part */
153 | }
154 | else {
155 | lua_Number n = luaL_checknumber(L, 1);
156 | /* integer part (rounds toward zero) */
157 | lua_Number ip = (n < 0) ? l_mathop(ceil)(n) : l_mathop(floor)(n);
158 | pushnumint(L, ip);
159 | /* fractional part (test needed for inf/-inf) */
160 | lua_pushnumber(L, (n == ip) ? l_mathop(0.0) : (n - ip));
161 | }
162 | return 2;
163 | }
164 |
165 |
166 | static int math_sqrt (lua_State *L) {
167 | lua_pushnumber(L, l_mathop(sqrt)(luaL_checknumber(L, 1)));
168 | return 1;
169 | }
170 |
171 |
172 | static int math_ult (lua_State *L) {
173 | lua_Integer a = luaL_checkinteger(L, 1);
174 | lua_Integer b = luaL_checkinteger(L, 2);
175 | lua_pushboolean(L, (lua_Unsigned)a < (lua_Unsigned)b);
176 | return 1;
177 | }
178 |
179 | static int math_log (lua_State *L) {
180 | lua_Number x = luaL_checknumber(L, 1);
181 | lua_Number res;
182 | if (lua_isnoneornil(L, 2))
183 | res = l_mathop(log)(x);
184 | else {
185 | lua_Number base = luaL_checknumber(L, 2);
186 | #if !defined(LUA_USE_C89)
187 | if (base == l_mathop(2.0))
188 | res = l_mathop(log2)(x); else
189 | #endif
190 | if (base == l_mathop(10.0))
191 | res = l_mathop(log10)(x);
192 | else
193 | res = l_mathop(log)(x)/l_mathop(log)(base);
194 | }
195 | lua_pushnumber(L, res);
196 | return 1;
197 | }
198 |
199 | static int math_exp (lua_State *L) {
200 | lua_pushnumber(L, l_mathop(exp)(luaL_checknumber(L, 1)));
201 | return 1;
202 | }
203 |
204 | static int math_deg (lua_State *L) {
205 | lua_pushnumber(L, luaL_checknumber(L, 1) * (l_mathop(180.0) / PI));
206 | return 1;
207 | }
208 |
209 | static int math_rad (lua_State *L) {
210 | lua_pushnumber(L, luaL_checknumber(L, 1) * (PI / l_mathop(180.0)));
211 | return 1;
212 | }
213 |
214 |
215 | static int math_min (lua_State *L) {
216 | int n = lua_gettop(L); /* number of arguments */
217 | int imin = 1; /* index of current minimum value */
218 | int i;
219 | luaL_argcheck(L, n >= 1, 1, "value expected");
220 | for (i = 2; i <= n; i++) {
221 | if (lua_compare(L, i, imin, LUA_OPLT))
222 | imin = i;
223 | }
224 | lua_pushvalue(L, imin);
225 | return 1;
226 | }
227 |
228 |
229 | static int math_max (lua_State *L) {
230 | int n = lua_gettop(L); /* number of arguments */
231 | int imax = 1; /* index of current maximum value */
232 | int i;
233 | luaL_argcheck(L, n >= 1, 1, "value expected");
234 | for (i = 2; i <= n; i++) {
235 | if (lua_compare(L, imax, i, LUA_OPLT))
236 | imax = i;
237 | }
238 | lua_pushvalue(L, imax);
239 | return 1;
240 | }
241 |
242 | /*
243 | ** This function uses 'double' (instead of 'lua_Number') to ensure that
244 | ** all bits from 'l_rand' can be represented, and that 'RANDMAX + 1.0'
245 | ** will keep full precision (ensuring that 'r' is always less than 1.0.)
246 | */
247 | static int math_random (lua_State *L) {
248 | lua_Integer low, up;
249 | double r = (double)l_rand() * (1.0 / ((double)L_RANDMAX + 1.0));
250 | switch (lua_gettop(L)) { /* check number of arguments */
251 | case 0: { /* no arguments */
252 | lua_pushnumber(L, (lua_Number)r); /* Number between 0 and 1 */
253 | return 1;
254 | }
255 | case 1: { /* only upper limit */
256 | low = 1;
257 | up = luaL_checkinteger(L, 1);
258 | break;
259 | }
260 | case 2: { /* lower and upper limits */
261 | low = luaL_checkinteger(L, 1);
262 | up = luaL_checkinteger(L, 2);
263 | break;
264 | }
265 | default: return luaL_error(L, "wrong number of arguments");
266 | }
267 | /* random integer in the interval [low, up] */
268 | luaL_argcheck(L, low <= up, 1, "interval is empty");
269 | luaL_argcheck(L, low >= 0 || up <= LUA_MAXINTEGER + low, 1,
270 | "interval too large");
271 | r *= (double)(up - low) + 1.0;
272 | lua_pushinteger(L, (lua_Integer)r + low);
273 | return 1;
274 | }
275 |
276 |
277 | static int math_randomseed (lua_State *L) {
278 | l_srand((unsigned int)(lua_Integer)luaL_checknumber(L, 1));
279 | (void)l_rand(); /* discard first value to avoid undesirable correlations */
280 | return 0;
281 | }
282 |
283 |
284 | static int math_type (lua_State *L) {
285 | if (lua_type(L, 1) == LUA_TNUMBER) {
286 | if (lua_isinteger(L, 1))
287 | lua_pushliteral(L, "integer");
288 | else
289 | lua_pushliteral(L, "float");
290 | }
291 | else {
292 | luaL_checkany(L, 1);
293 | lua_pushnil(L);
294 | }
295 | return 1;
296 | }
297 |
298 |
299 | /*
300 | ** {==================================================================
301 | ** Deprecated functions (for compatibility only)
302 | ** ===================================================================
303 | */
304 | #if defined(LUA_COMPAT_MATHLIB)
305 |
306 | static int math_cosh (lua_State *L) {
307 | lua_pushnumber(L, l_mathop(cosh)(luaL_checknumber(L, 1)));
308 | return 1;
309 | }
310 |
311 | static int math_sinh (lua_State *L) {
312 | lua_pushnumber(L, l_mathop(sinh)(luaL_checknumber(L, 1)));
313 | return 1;
314 | }
315 |
316 | static int math_tanh (lua_State *L) {
317 | lua_pushnumber(L, l_mathop(tanh)(luaL_checknumber(L, 1)));
318 | return 1;
319 | }
320 |
321 | static int math_pow (lua_State *L) {
322 | lua_Number x = luaL_checknumber(L, 1);
323 | lua_Number y = luaL_checknumber(L, 2);
324 | lua_pushnumber(L, l_mathop(pow)(x, y));
325 | return 1;
326 | }
327 |
328 | static int math_frexp (lua_State *L) {
329 | int e;
330 | lua_pushnumber(L, l_mathop(frexp)(luaL_checknumber(L, 1), &e));
331 | lua_pushinteger(L, e);
332 | return 2;
333 | }
334 |
335 | static int math_ldexp (lua_State *L) {
336 | lua_Number x = luaL_checknumber(L, 1);
337 | int ep = (int)luaL_checkinteger(L, 2);
338 | lua_pushnumber(L, l_mathop(ldexp)(x, ep));
339 | return 1;
340 | }
341 |
342 | static int math_log10 (lua_State *L) {
343 | lua_pushnumber(L, l_mathop(log10)(luaL_checknumber(L, 1)));
344 | return 1;
345 | }
346 |
347 | #endif
348 | /* }================================================================== */
349 |
350 |
351 |
352 | static const luaL_Reg mathlib[] = {
353 | {"abs", math_abs},
354 | {"acos", math_acos},
355 | {"asin", math_asin},
356 | {"atan", math_atan},
357 | {"ceil", math_ceil},
358 | {"cos", math_cos},
359 | {"deg", math_deg},
360 | {"exp", math_exp},
361 | {"tointeger", math_toint},
362 | {"floor", math_floor},
363 | {"fmod", math_fmod},
364 | {"ult", math_ult},
365 | {"log", math_log},
366 | {"max", math_max},
367 | {"min", math_min},
368 | {"modf", math_modf},
369 | {"rad", math_rad},
370 | {"random", math_random},
371 | {"randomseed", math_randomseed},
372 | {"sin", math_sin},
373 | {"sqrt", math_sqrt},
374 | {"tan", math_tan},
375 | {"type", math_type},
376 | #if defined(LUA_COMPAT_MATHLIB)
377 | {"atan2", math_atan},
378 | {"cosh", math_cosh},
379 | {"sinh", math_sinh},
380 | {"tanh", math_tanh},
381 | {"pow", math_pow},
382 | {"frexp", math_frexp},
383 | {"ldexp", math_ldexp},
384 | {"log10", math_log10},
385 | #endif
386 | /* placeholders */
387 | {"pi", NULL},
388 | {"huge", NULL},
389 | {"maxinteger", NULL},
390 | {"mininteger", NULL},
391 | {NULL, NULL}
392 | };
393 |
394 |
395 | /*
396 | ** Open math library
397 | */
398 | LUAMOD_API int luaopen_math (lua_State *L) {
399 | luaL_newlib(L, mathlib);
400 | lua_pushnumber(L, PI);
401 | lua_setfield(L, -2, "pi");
402 | lua_pushnumber(L, (lua_Number)HUGE_VAL);
403 | lua_setfield(L, -2, "huge");
404 | lua_pushinteger(L, LUA_MAXINTEGER);
405 | lua_setfield(L, -2, "maxinteger");
406 | lua_pushinteger(L, LUA_MININTEGER);
407 | lua_setfield(L, -2, "mininteger");
408 | return 1;
409 | }
410 |
411 |
--------------------------------------------------------------------------------
/lualib/src/main/cpp/lua/lmem.c:
--------------------------------------------------------------------------------
1 | /*
2 | ** $Id: lmem.c,v 1.91 2015/03/06 19:45:54 roberto Exp $
3 | ** Interface to Memory Manager
4 | ** See Copyright Notice in lua.h
5 | */
6 |
7 | #define lmem_c
8 | #define LUA_CORE
9 |
10 | #include "lprefix.h"
11 |
12 |
13 | #include
14 |
15 | #include "lua.h"
16 |
17 | #include "ldebug.h"
18 | #include "ldo.h"
19 | #include "lgc.h"
20 | #include "lmem.h"
21 | #include "lobject.h"
22 | #include "lstate.h"
23 |
24 |
25 |
26 | /*
27 | ** About the realloc function:
28 | ** void * frealloc (void *ud, void *ptr, size_t osize, size_t nsize);
29 | ** ('osize' is the old size, 'nsize' is the new size)
30 | **
31 | ** * frealloc(ud, NULL, x, s) creates a new block of size 's' (no
32 | ** matter 'x').
33 | **
34 | ** * frealloc(ud, p, x, 0) frees the block 'p'
35 | ** (in this specific case, frealloc must return NULL);
36 | ** particularly, frealloc(ud, NULL, 0, 0) does nothing
37 | ** (which is equivalent to free(NULL) in ISO C)
38 | **
39 | ** frealloc returns NULL if it cannot create or reallocate the area
40 | ** (any reallocation to an equal or smaller size cannot fail!)
41 | */
42 |
43 |
44 |
45 | #define MINSIZEARRAY 4
46 |
47 |
48 | void *luaM_growaux_ (lua_State *L, void *block, int *size, size_t size_elems,
49 | int limit, const char *what) {
50 | void *newblock;
51 | int newsize;
52 | if (*size >= limit/2) { /* cannot double it? */
53 | if (*size >= limit) /* cannot grow even a little? */
54 | luaG_runerror(L, "too many %s (limit is %d)", what, limit);
55 | newsize = limit; /* still have at least one free place */
56 | }
57 | else {
58 | newsize = (*size)*2;
59 | if (newsize < MINSIZEARRAY)
60 | newsize = MINSIZEARRAY; /* minimum size */
61 | }
62 | newblock = luaM_reallocv(L, block, *size, newsize, size_elems);
63 | *size = newsize; /* update only when everything else is OK */
64 | return newblock;
65 | }
66 |
67 |
68 | l_noret luaM_toobig (lua_State *L) {
69 | luaG_runerror(L, "memory allocation error: block too big");
70 | }
71 |
72 |
73 |
74 | /*
75 | ** generic allocation routine.
76 | */
77 | void *luaM_realloc_ (lua_State *L, void *block, size_t osize, size_t nsize) {
78 | void *newblock;
79 | global_State *g = G(L);
80 | size_t realosize = (block) ? osize : 0;
81 | lua_assert((realosize == 0) == (block == NULL));
82 | #if defined(HARDMEMTESTS)
83 | if (nsize > realosize && g->gcrunning)
84 | luaC_fullgc(L, 1); /* force a GC whenever possible */
85 | #endif
86 | newblock = (*g->frealloc)(g->ud, block, osize, nsize);
87 | if (newblock == NULL && nsize > 0) {
88 | lua_assert(nsize > realosize); /* cannot fail when shrinking a block */
89 | if (g->version) { /* is state fully built? */
90 | luaC_fullgc(L, 1); /* try to free some memory... */
91 | newblock = (*g->frealloc)(g->ud, block, osize, nsize); /* try again */
92 | }
93 | if (newblock == NULL)
94 | luaD_throw(L, LUA_ERRMEM);
95 | }
96 | lua_assert((nsize == 0) == (newblock == NULL));
97 | g->GCdebt = (g->GCdebt + nsize) - realosize;
98 | return newblock;
99 | }
100 |
101 |
--------------------------------------------------------------------------------
/lualib/src/main/cpp/lua/lmem.h:
--------------------------------------------------------------------------------
1 | /*
2 | ** $Id: lmem.h,v 1.43 2014/12/19 17:26:14 roberto Exp $
3 | ** Interface to Memory Manager
4 | ** See Copyright Notice in lua.h
5 | */
6 |
7 | #ifndef lmem_h
8 | #define lmem_h
9 |
10 |
11 | #include
12 |
13 | #include "llimits.h"
14 | #include "lua.h"
15 |
16 |
17 | /*
18 | ** This macro reallocs a vector 'b' from 'on' to 'n' elements, where
19 | ** each element has size 'e'. In case of arithmetic overflow of the
20 | ** product 'n'*'e', it raises an error (calling 'luaM_toobig'). Because
21 | ** 'e' is always constant, it avoids the runtime division MAX_SIZET/(e).
22 | **
23 | ** (The macro is somewhat complex to avoid warnings: The 'sizeof'
24 | ** comparison avoids a runtime comparison when overflow cannot occur.
25 | ** The compiler should be able to optimize the real test by itself, but
26 | ** when it does it, it may give a warning about "comparison is always
27 | ** false due to limited range of data type"; the +1 tricks the compiler,
28 | ** avoiding this warning but also this optimization.)
29 | */
30 | #define luaM_reallocv(L,b,on,n,e) \
31 | (((sizeof(n) >= sizeof(size_t) && cast(size_t, (n)) + 1 > MAX_SIZET/(e)) \
32 | ? luaM_toobig(L) : cast_void(0)) , \
33 | luaM_realloc_(L, (b), (on)*(e), (n)*(e)))
34 |
35 | /*
36 | ** Arrays of chars do not need any test
37 | */
38 | #define luaM_reallocvchar(L,b,on,n) \
39 | cast(char *, luaM_realloc_(L, (b), (on)*sizeof(char), (n)*sizeof(char)))
40 |
41 | #define luaM_freemem(L, b, s) luaM_realloc_(L, (b), (s), 0)
42 | #define luaM_free(L, b) luaM_realloc_(L, (b), sizeof(*(b)), 0)
43 | #define luaM_freearray(L, b, n) luaM_realloc_(L, (b), (n)*sizeof(*(b)), 0)
44 |
45 | #define luaM_malloc(L,s) luaM_realloc_(L, NULL, 0, (s))
46 | #define luaM_new(L,t) cast(t *, luaM_malloc(L, sizeof(t)))
47 | #define luaM_newvector(L,n,t) \
48 | cast(t *, luaM_reallocv(L, NULL, 0, n, sizeof(t)))
49 |
50 | #define luaM_newobject(L,tag,s) luaM_realloc_(L, NULL, tag, (s))
51 |
52 | #define luaM_growvector(L,v,nelems,size,t,limit,e) \
53 | if ((nelems)+1 > (size)) \
54 | ((v)=cast(t *, luaM_growaux_(L,v,&(size),sizeof(t),limit,e)))
55 |
56 | #define luaM_reallocvector(L, v,oldn,n,t) \
57 | ((v)=cast(t *, luaM_reallocv(L, v, oldn, n, sizeof(t))))
58 |
59 | LUAI_FUNC l_noret luaM_toobig (lua_State *L);
60 |
61 | /* not to be called directly */
62 | LUAI_FUNC void *luaM_realloc_ (lua_State *L, void *block, size_t oldsize,
63 | size_t size);
64 | LUAI_FUNC void *luaM_growaux_ (lua_State *L, void *block, int *size,
65 | size_t size_elem, int limit,
66 | const char *what);
67 |
68 | #endif
69 |
70 |
--------------------------------------------------------------------------------
/lualib/src/main/cpp/lua/lopcodes.c:
--------------------------------------------------------------------------------
1 | /*
2 | ** $Id: lopcodes.c,v 1.55 2015/01/05 13:48:33 roberto Exp $
3 | ** Opcodes for Lua virtual machine
4 | ** See Copyright Notice in lua.h
5 | */
6 |
7 | #define lopcodes_c
8 | #define LUA_CORE
9 |
10 | #include "lprefix.h"
11 |
12 |
13 | #include
14 |
15 | #include "lopcodes.h"
16 |
17 |
18 | /* ORDER OP */
19 |
20 | LUAI_DDEF const char *const luaP_opnames[NUM_OPCODES+1] = {
21 | "MOVE",
22 | "LOADK",
23 | "LOADKX",
24 | "LOADBOOL",
25 | "LOADNIL",
26 | "GETUPVAL",
27 | "GETTABUP",
28 | "GETTABLE",
29 | "SETTABUP",
30 | "SETUPVAL",
31 | "SETTABLE",
32 | "NEWTABLE",
33 | "SELF",
34 | "ADD",
35 | "SUB",
36 | "MUL",
37 | "MOD",
38 | "POW",
39 | "DIV",
40 | "IDIV",
41 | "BAND",
42 | "BOR",
43 | "BXOR",
44 | "SHL",
45 | "SHR",
46 | "UNM",
47 | "BNOT",
48 | "NOT",
49 | "LEN",
50 | "CONCAT",
51 | "JMP",
52 | "EQ",
53 | "LT",
54 | "LE",
55 | "TEST",
56 | "TESTSET",
57 | "CALL",
58 | "TAILCALL",
59 | "RETURN",
60 | "FORLOOP",
61 | "FORPREP",
62 | "TFORCALL",
63 | "TFORLOOP",
64 | "SETLIST",
65 | "CLOSURE",
66 | "VARARG",
67 | "EXTRAARG",
68 | NULL
69 | };
70 |
71 |
72 | #define opmode(t,a,b,c,m) (((t)<<7) | ((a)<<6) | ((b)<<4) | ((c)<<2) | (m))
73 |
74 | LUAI_DDEF const lu_byte luaP_opmodes[NUM_OPCODES] = {
75 | /* T A B C mode opcode */
76 | opmode(0, 1, OpArgR, OpArgN, iABC) /* OP_MOVE */
77 | ,opmode(0, 1, OpArgK, OpArgN, iABx) /* OP_LOADK */
78 | ,opmode(0, 1, OpArgN, OpArgN, iABx) /* OP_LOADKX */
79 | ,opmode(0, 1, OpArgU, OpArgU, iABC) /* OP_LOADBOOL */
80 | ,opmode(0, 1, OpArgU, OpArgN, iABC) /* OP_LOADNIL */
81 | ,opmode(0, 1, OpArgU, OpArgN, iABC) /* OP_GETUPVAL */
82 | ,opmode(0, 1, OpArgU, OpArgK, iABC) /* OP_GETTABUP */
83 | ,opmode(0, 1, OpArgR, OpArgK, iABC) /* OP_GETTABLE */
84 | ,opmode(0, 0, OpArgK, OpArgK, iABC) /* OP_SETTABUP */
85 | ,opmode(0, 0, OpArgU, OpArgN, iABC) /* OP_SETUPVAL */
86 | ,opmode(0, 0, OpArgK, OpArgK, iABC) /* OP_SETTABLE */
87 | ,opmode(0, 1, OpArgU, OpArgU, iABC) /* OP_NEWTABLE */
88 | ,opmode(0, 1, OpArgR, OpArgK, iABC) /* OP_SELF */
89 | ,opmode(0, 1, OpArgK, OpArgK, iABC) /* OP_ADD */
90 | ,opmode(0, 1, OpArgK, OpArgK, iABC) /* OP_SUB */
91 | ,opmode(0, 1, OpArgK, OpArgK, iABC) /* OP_MUL */
92 | ,opmode(0, 1, OpArgK, OpArgK, iABC) /* OP_MOD */
93 | ,opmode(0, 1, OpArgK, OpArgK, iABC) /* OP_POW */
94 | ,opmode(0, 1, OpArgK, OpArgK, iABC) /* OP_DIV */
95 | ,opmode(0, 1, OpArgK, OpArgK, iABC) /* OP_IDIV */
96 | ,opmode(0, 1, OpArgK, OpArgK, iABC) /* OP_BAND */
97 | ,opmode(0, 1, OpArgK, OpArgK, iABC) /* OP_BOR */
98 | ,opmode(0, 1, OpArgK, OpArgK, iABC) /* OP_BXOR */
99 | ,opmode(0, 1, OpArgK, OpArgK, iABC) /* OP_SHL */
100 | ,opmode(0, 1, OpArgK, OpArgK, iABC) /* OP_SHR */
101 | ,opmode(0, 1, OpArgR, OpArgN, iABC) /* OP_UNM */
102 | ,opmode(0, 1, OpArgR, OpArgN, iABC) /* OP_BNOT */
103 | ,opmode(0, 1, OpArgR, OpArgN, iABC) /* OP_NOT */
104 | ,opmode(0, 1, OpArgR, OpArgN, iABC) /* OP_LEN */
105 | ,opmode(0, 1, OpArgR, OpArgR, iABC) /* OP_CONCAT */
106 | ,opmode(0, 0, OpArgR, OpArgN, iAsBx) /* OP_JMP */
107 | ,opmode(1, 0, OpArgK, OpArgK, iABC) /* OP_EQ */
108 | ,opmode(1, 0, OpArgK, OpArgK, iABC) /* OP_LT */
109 | ,opmode(1, 0, OpArgK, OpArgK, iABC) /* OP_LE */
110 | ,opmode(1, 0, OpArgN, OpArgU, iABC) /* OP_TEST */
111 | ,opmode(1, 1, OpArgR, OpArgU, iABC) /* OP_TESTSET */
112 | ,opmode(0, 1, OpArgU, OpArgU, iABC) /* OP_CALL */
113 | ,opmode(0, 1, OpArgU, OpArgU, iABC) /* OP_TAILCALL */
114 | ,opmode(0, 0, OpArgU, OpArgN, iABC) /* OP_RETURN */
115 | ,opmode(0, 1, OpArgR, OpArgN, iAsBx) /* OP_FORLOOP */
116 | ,opmode(0, 1, OpArgR, OpArgN, iAsBx) /* OP_FORPREP */
117 | ,opmode(0, 0, OpArgN, OpArgU, iABC) /* OP_TFORCALL */
118 | ,opmode(0, 1, OpArgR, OpArgN, iAsBx) /* OP_TFORLOOP */
119 | ,opmode(0, 0, OpArgU, OpArgU, iABC) /* OP_SETLIST */
120 | ,opmode(0, 1, OpArgU, OpArgN, iABx) /* OP_CLOSURE */
121 | ,opmode(0, 1, OpArgU, OpArgN, iABC) /* OP_VARARG */
122 | ,opmode(0, 0, OpArgU, OpArgU, iAx) /* OP_EXTRAARG */
123 | };
124 |
125 |
--------------------------------------------------------------------------------
/lualib/src/main/cpp/lua/lopcodes.h:
--------------------------------------------------------------------------------
1 | /*
2 | ** $Id: lopcodes.h,v 1.149 2016/07/19 17:12:21 roberto Exp $
3 | ** Opcodes for Lua virtual machine
4 | ** See Copyright Notice in lua.h
5 | */
6 |
7 | #ifndef lopcodes_h
8 | #define lopcodes_h
9 |
10 | #include "llimits.h"
11 |
12 |
13 | /*===========================================================================
14 | We assume that instructions are unsigned numbers.
15 | All instructions have an opcode in the first 6 bits.
16 | Instructions can have the following fields:
17 | 'A' : 8 bits
18 | 'B' : 9 bits
19 | 'C' : 9 bits
20 | 'Ax' : 26 bits ('A', 'B', and 'C' together)
21 | 'Bx' : 18 bits ('B' and 'C' together)
22 | 'sBx' : signed Bx
23 |
24 | A signed argument is represented in excess K; that is, the number
25 | value is the unsigned value minus K. K is exactly the maximum value
26 | for that argument (so that -max is represented by 0, and +max is
27 | represented by 2*max), which is half the maximum for the corresponding
28 | unsigned argument.
29 | ===========================================================================*/
30 |
31 |
32 | enum OpMode {iABC, iABx, iAsBx, iAx}; /* basic instruction format */
33 |
34 |
35 | /*
36 | ** size and position of opcode arguments.
37 | */
38 | #define SIZE_C 9
39 | #define SIZE_B 9
40 | #define SIZE_Bx (SIZE_C + SIZE_B)
41 | #define SIZE_A 8
42 | #define SIZE_Ax (SIZE_C + SIZE_B + SIZE_A)
43 |
44 | #define SIZE_OP 6
45 |
46 | #define POS_OP 0
47 | #define POS_A (POS_OP + SIZE_OP)
48 | #define POS_C (POS_A + SIZE_A)
49 | #define POS_B (POS_C + SIZE_C)
50 | #define POS_Bx POS_C
51 | #define POS_Ax POS_A
52 |
53 |
54 | /*
55 | ** limits for opcode arguments.
56 | ** we use (signed) int to manipulate most arguments,
57 | ** so they must fit in LUAI_BITSINT-1 bits (-1 for sign)
58 | */
59 | #if SIZE_Bx < LUAI_BITSINT-1
60 | #define MAXARG_Bx ((1<>1) /* 'sBx' is signed */
62 | #else
63 | #define MAXARG_Bx MAX_INT
64 | #define MAXARG_sBx MAX_INT
65 | #endif
66 |
67 | #if SIZE_Ax < LUAI_BITSINT-1
68 | #define MAXARG_Ax ((1<>POS_OP) & MASK1(SIZE_OP,0)))
90 | #define SET_OPCODE(i,o) ((i) = (((i)&MASK0(SIZE_OP,POS_OP)) | \
91 | ((cast(Instruction, o)<>pos) & MASK1(size,0)))
94 | #define setarg(i,v,pos,size) ((i) = (((i)&MASK0(size,pos)) | \
95 | ((cast(Instruction, v)<> RK(C) */
201 | OP_UNM,/* A B R(A) := -R(B) */
202 | OP_BNOT,/* A B R(A) := ~R(B) */
203 | OP_NOT,/* A B R(A) := not R(B) */
204 | OP_LEN,/* A B R(A) := length of R(B) */
205 |
206 | OP_CONCAT,/* A B C R(A) := R(B).. ... ..R(C) */
207 |
208 | OP_JMP,/* A sBx pc+=sBx; if (A) close all upvalues >= R(A - 1) */
209 | OP_EQ,/* A B C if ((RK(B) == RK(C)) ~= A) then pc++ */
210 | OP_LT,/* A B C if ((RK(B) < RK(C)) ~= A) then pc++ */
211 | OP_LE,/* A B C if ((RK(B) <= RK(C)) ~= A) then pc++ */
212 |
213 | OP_TEST,/* A C if not (R(A) <=> C) then pc++ */
214 | OP_TESTSET,/* A B C if (R(B) <=> C) then R(A) := R(B) else pc++ */
215 |
216 | OP_CALL,/* A B C R(A), ... ,R(A+C-2) := R(A)(R(A+1), ... ,R(A+B-1)) */
217 | OP_TAILCALL,/* A B C return R(A)(R(A+1), ... ,R(A+B-1)) */
218 | OP_RETURN,/* A B return R(A), ... ,R(A+B-2) (see note) */
219 |
220 | OP_FORLOOP,/* A sBx R(A)+=R(A+2);
221 | if R(A) = R(A+1) then { pc+=sBx; R(A+3)=R(A) }*/
222 | OP_FORPREP,/* A sBx R(A)-=R(A+2); pc+=sBx */
223 |
224 | OP_TFORCALL,/* A C R(A+3), ... ,R(A+2+C) := R(A)(R(A+1), R(A+2)); */
225 | OP_TFORLOOP,/* A sBx if R(A+1) ~= nil then { R(A)=R(A+1); pc += sBx }*/
226 |
227 | OP_SETLIST,/* A B C R(A)[(C-1)*FPF+i] := R(A+i), 1 <= i <= B */
228 |
229 | OP_CLOSURE,/* A Bx R(A) := closure(KPROTO[Bx]) */
230 |
231 | OP_VARARG,/* A B R(A), R(A+1), ..., R(A+B-2) = vararg */
232 |
233 | OP_EXTRAARG/* Ax extra (larger) argument for previous opcode */
234 | } OpCode;
235 |
236 |
237 | #define NUM_OPCODES (cast(int, OP_EXTRAARG) + 1)
238 |
239 |
240 |
241 | /*===========================================================================
242 | Notes:
243 | (*) In OP_CALL, if (B == 0) then B = top. If (C == 0), then 'top' is
244 | set to last_result+1, so next open instruction (OP_CALL, OP_RETURN,
245 | OP_SETLIST) may use 'top'.
246 |
247 | (*) In OP_VARARG, if (B == 0) then use actual number of varargs and
248 | set top (like in OP_CALL with C == 0).
249 |
250 | (*) In OP_RETURN, if (B == 0) then return up to 'top'.
251 |
252 | (*) In OP_SETLIST, if (B == 0) then B = 'top'; if (C == 0) then next
253 | 'instruction' is EXTRAARG(real C).
254 |
255 | (*) In OP_LOADKX, the next 'instruction' is always EXTRAARG.
256 |
257 | (*) For comparisons, A specifies what condition the test should accept
258 | (true or false).
259 |
260 | (*) All 'skips' (pc++) assume that next instruction is a jump.
261 |
262 | ===========================================================================*/
263 |
264 |
265 | /*
266 | ** masks for instruction properties. The format is:
267 | ** bits 0-1: op mode
268 | ** bits 2-3: C arg mode
269 | ** bits 4-5: B arg mode
270 | ** bit 6: instruction set register A
271 | ** bit 7: operator is a test (next instruction must be a jump)
272 | */
273 |
274 | enum OpArgMask {
275 | OpArgN, /* argument is not used */
276 | OpArgU, /* argument is used */
277 | OpArgR, /* argument is a register or a jump offset */
278 | OpArgK /* argument is a constant or register/constant */
279 | };
280 |
281 | LUAI_DDEC const lu_byte luaP_opmodes[NUM_OPCODES];
282 |
283 | #define getOpMode(m) (cast(enum OpMode, luaP_opmodes[m] & 3))
284 | #define getBMode(m) (cast(enum OpArgMask, (luaP_opmodes[m] >> 4) & 3))
285 | #define getCMode(m) (cast(enum OpArgMask, (luaP_opmodes[m] >> 2) & 3))
286 | #define testAMode(m) (luaP_opmodes[m] & (1 << 6))
287 | #define testTMode(m) (luaP_opmodes[m] & (1 << 7))
288 |
289 |
290 | LUAI_DDEC const char *const luaP_opnames[NUM_OPCODES+1]; /* opcode names */
291 |
292 |
293 | /* number of list items to accumulate before a SETLIST instruction */
294 | #define LFIELDS_PER_FLUSH 50
295 |
296 |
297 | #endif
298 |
--------------------------------------------------------------------------------
/lualib/src/main/cpp/lua/loslib.c:
--------------------------------------------------------------------------------
1 | /*
2 | ** $Id: loslib.c,v 1.65 2016/07/18 17:58:58 roberto Exp $
3 | ** Standard Operating System library
4 | ** See Copyright Notice in lua.h
5 | */
6 |
7 | #define loslib_c
8 | #define LUA_LIB
9 |
10 | #include "lprefix.h"
11 |
12 |
13 | #include
14 | #include
15 | #include
16 | #include
17 | #include
18 |
19 | #include "lua.h"
20 |
21 | #include "lauxlib.h"
22 | #include "lualib.h"
23 |
24 |
25 | /*
26 | ** {==================================================================
27 | ** List of valid conversion specifiers for the 'strftime' function;
28 | ** options are grouped by length; group of length 2 start with '||'.
29 | ** ===================================================================
30 | */
31 | #if !defined(LUA_STRFTIMEOPTIONS) /* { */
32 |
33 | /* options for ANSI C 89 (only 1-char options) */
34 | #define L_STRFTIMEC89 "aAbBcdHIjmMpSUwWxXyYZ%"
35 |
36 | /* options for ISO C 99 and POSIX */
37 | #define L_STRFTIMEC99 "aAbBcCdDeFgGhHIjmMnprRStTuUVwWxXyYzZ%" \
38 | "||" "EcECExEXEyEY" "OdOeOHOIOmOMOSOuOUOVOwOWOy" /* two-char options */
39 |
40 | /* options for Windows */
41 | #define L_STRFTIMEWIN "aAbBcdHIjmMpSUwWxXyYzZ%" \
42 | "||" "#c#x#d#H#I#j#m#M#S#U#w#W#y#Y" /* two-char options */
43 |
44 | #if defined(LUA_USE_WINDOWS)
45 | #define LUA_STRFTIMEOPTIONS L_STRFTIMEWIN
46 | #elif defined(LUA_USE_C89)
47 | #define LUA_STRFTIMEOPTIONS L_STRFTIMEC89
48 | #else /* C99 specification */
49 | #define LUA_STRFTIMEOPTIONS L_STRFTIMEC99
50 | #endif
51 |
52 | #endif /* } */
53 | /* }================================================================== */
54 |
55 |
56 | /*
57 | ** {==================================================================
58 | ** Configuration for time-related stuff
59 | ** ===================================================================
60 | */
61 |
62 | #if !defined(l_time_t) /* { */
63 | /*
64 | ** type to represent time_t in Lua
65 | */
66 | #define l_timet lua_Integer
67 | #define l_pushtime(L,t) lua_pushinteger(L,(lua_Integer)(t))
68 |
69 | static time_t l_checktime (lua_State *L, int arg) {
70 | lua_Integer t = luaL_checkinteger(L, arg);
71 | luaL_argcheck(L, (time_t)t == t, arg, "time out-of-bounds");
72 | return (time_t)t;
73 | }
74 |
75 | #endif /* } */
76 |
77 |
78 | #if !defined(l_gmtime) /* { */
79 | /*
80 | ** By default, Lua uses gmtime/localtime, except when POSIX is available,
81 | ** where it uses gmtime_r/localtime_r
82 | */
83 |
84 | #if defined(LUA_USE_POSIX) /* { */
85 |
86 | #define l_gmtime(t,r) gmtime_r(t,r)
87 | #define l_localtime(t,r) localtime_r(t,r)
88 |
89 | #else /* }{ */
90 |
91 | /* ISO C definitions */
92 | #define l_gmtime(t,r) ((void)(r)->tm_sec, gmtime(t))
93 | #define l_localtime(t,r) ((void)(r)->tm_sec, localtime(t))
94 |
95 | #endif /* } */
96 |
97 | #endif /* } */
98 |
99 | /* }================================================================== */
100 |
101 |
102 | /*
103 | ** {==================================================================
104 | ** Configuration for 'tmpnam':
105 | ** By default, Lua uses tmpnam except when POSIX is available, where
106 | ** it uses mkstemp.
107 | ** ===================================================================
108 | */
109 | #if !defined(lua_tmpnam) /* { */
110 |
111 | #if defined(LUA_USE_POSIX) /* { */
112 |
113 | #include
114 |
115 | #define LUA_TMPNAMBUFSIZE 32
116 |
117 | #if !defined(LUA_TMPNAMTEMPLATE)
118 | #define LUA_TMPNAMTEMPLATE "/tmp/lua_XXXXXX"
119 | #endif
120 |
121 | #define lua_tmpnam(b,e) { \
122 | strcpy(b, LUA_TMPNAMTEMPLATE); \
123 | e = mkstemp(b); \
124 | if (e != -1) close(e); \
125 | e = (e == -1); }
126 |
127 | #else /* }{ */
128 |
129 | /* ISO C definitions */
130 | #define LUA_TMPNAMBUFSIZE L_tmpnam
131 | #define lua_tmpnam(b,e) { e = (tmpnam(b) == NULL); }
132 |
133 | #endif /* } */
134 |
135 | #endif /* } */
136 | /* }================================================================== */
137 |
138 |
139 |
140 |
141 | static int os_execute (lua_State *L) {
142 | const char *cmd = luaL_optstring(L, 1, NULL);
143 | int stat = system(cmd);
144 | if (cmd != NULL)
145 | return luaL_execresult(L, stat);
146 | else {
147 | lua_pushboolean(L, stat); /* true if there is a shell */
148 | return 1;
149 | }
150 | }
151 |
152 |
153 | static int os_remove (lua_State *L) {
154 | const char *filename = luaL_checkstring(L, 1);
155 | return luaL_fileresult(L, remove(filename) == 0, filename);
156 | }
157 |
158 |
159 | static int os_rename (lua_State *L) {
160 | const char *fromname = luaL_checkstring(L, 1);
161 | const char *toname = luaL_checkstring(L, 2);
162 | return luaL_fileresult(L, rename(fromname, toname) == 0, NULL);
163 | }
164 |
165 |
166 | static int os_tmpname (lua_State *L) {
167 | char buff[LUA_TMPNAMBUFSIZE];
168 | int err;
169 | lua_tmpnam(buff, err);
170 | if (err)
171 | return luaL_error(L, "unable to generate a unique filename");
172 | lua_pushstring(L, buff);
173 | return 1;
174 | }
175 |
176 |
177 | static int os_getenv (lua_State *L) {
178 | lua_pushstring(L, getenv(luaL_checkstring(L, 1))); /* if NULL push nil */
179 | return 1;
180 | }
181 |
182 |
183 | static int os_clock (lua_State *L) {
184 | lua_pushnumber(L, ((lua_Number)clock())/(lua_Number)CLOCKS_PER_SEC);
185 | return 1;
186 | }
187 |
188 |
189 | /*
190 | ** {======================================================
191 | ** Time/Date operations
192 | ** { year=%Y, month=%m, day=%d, hour=%H, min=%M, sec=%S,
193 | ** wday=%w+1, yday=%j, isdst=? }
194 | ** =======================================================
195 | */
196 |
197 | static void setfield (lua_State *L, const char *key, int value) {
198 | lua_pushinteger(L, value);
199 | lua_setfield(L, -2, key);
200 | }
201 |
202 | static void setboolfield (lua_State *L, const char *key, int value) {
203 | if (value < 0) /* undefined? */
204 | return; /* does not set field */
205 | lua_pushboolean(L, value);
206 | lua_setfield(L, -2, key);
207 | }
208 |
209 |
210 | /*
211 | ** Set all fields from structure 'tm' in the table on top of the stack
212 | */
213 | static void setallfields (lua_State *L, struct tm *stm) {
214 | setfield(L, "sec", stm->tm_sec);
215 | setfield(L, "min", stm->tm_min);
216 | setfield(L, "hour", stm->tm_hour);
217 | setfield(L, "day", stm->tm_mday);
218 | setfield(L, "month", stm->tm_mon + 1);
219 | setfield(L, "year", stm->tm_year + 1900);
220 | setfield(L, "wday", stm->tm_wday + 1);
221 | setfield(L, "yday", stm->tm_yday + 1);
222 | setboolfield(L, "isdst", stm->tm_isdst);
223 | }
224 |
225 |
226 | static int getboolfield (lua_State *L, const char *key) {
227 | int res;
228 | res = (lua_getfield(L, -1, key) == LUA_TNIL) ? -1 : lua_toboolean(L, -1);
229 | lua_pop(L, 1);
230 | return res;
231 | }
232 |
233 |
234 | /* maximum value for date fields (to avoid arithmetic overflows with 'int') */
235 | #if !defined(L_MAXDATEFIELD)
236 | #define L_MAXDATEFIELD (INT_MAX / 2)
237 | #endif
238 |
239 | static int getfield (lua_State *L, const char *key, int d, int delta) {
240 | int isnum;
241 | int t = lua_getfield(L, -1, key); /* get field and its type */
242 | lua_Integer res = lua_tointegerx(L, -1, &isnum);
243 | if (!isnum) { /* field is not an integer? */
244 | if (t != LUA_TNIL) /* some other value? */
245 | return luaL_error(L, "field '%s' is not an integer", key);
246 | else if (d < 0) /* absent field; no default? */
247 | return luaL_error(L, "field '%s' missing in date table", key);
248 | res = d;
249 | }
250 | else {
251 | if (!(-L_MAXDATEFIELD <= res && res <= L_MAXDATEFIELD))
252 | return luaL_error(L, "field '%s' is out-of-bound", key);
253 | res -= delta;
254 | }
255 | lua_pop(L, 1);
256 | return (int)res;
257 | }
258 |
259 |
260 | static const char *checkoption (lua_State *L, const char *conv,
261 | ptrdiff_t convlen, char *buff) {
262 | const char *option = LUA_STRFTIMEOPTIONS;
263 | int oplen = 1; /* length of options being checked */
264 | for (; *option != '\0' && oplen <= convlen; option += oplen) {
265 | if (*option == '|') /* next block? */
266 | oplen++; /* will check options with next length (+1) */
267 | else if (memcmp(conv, option, oplen) == 0) { /* match? */
268 | memcpy(buff, conv, oplen); /* copy valid option to buffer */
269 | buff[oplen] = '\0';
270 | return conv + oplen; /* return next item */
271 | }
272 | }
273 | luaL_argerror(L, 1,
274 | lua_pushfstring(L, "invalid conversion specifier '%%%s'", conv));
275 | return conv; /* to avoid warnings */
276 | }
277 |
278 |
279 | /* maximum size for an individual 'strftime' item */
280 | #define SIZETIMEFMT 250
281 |
282 |
283 | static int os_date (lua_State *L) {
284 | size_t slen;
285 | const char *s = luaL_optlstring(L, 1, "%c", &slen);
286 | time_t t = luaL_opt(L, l_checktime, 2, time(NULL));
287 | const char *se = s + slen; /* 's' end */
288 | struct tm tmr, *stm;
289 | if (*s == '!') { /* UTC? */
290 | stm = l_gmtime(&t, &tmr);
291 | s++; /* skip '!' */
292 | }
293 | else
294 | stm = l_localtime(&t, &tmr);
295 | if (stm == NULL) /* invalid date? */
296 | luaL_error(L, "time result cannot be represented in this installation");
297 | if (strcmp(s, "*t") == 0) {
298 | lua_createtable(L, 0, 9); /* 9 = number of fields */
299 | setallfields(L, stm);
300 | }
301 | else {
302 | char cc[4]; /* buffer for individual conversion specifiers */
303 | luaL_Buffer b;
304 | cc[0] = '%';
305 | luaL_buffinit(L, &b);
306 | while (s < se) {
307 | if (*s != '%') /* not a conversion specifier? */
308 | luaL_addchar(&b, *s++);
309 | else {
310 | size_t reslen;
311 | char *buff = luaL_prepbuffsize(&b, SIZETIMEFMT);
312 | s++; /* skip '%' */
313 | s = checkoption(L, s, se - s, cc + 1); /* copy specifier to 'cc' */
314 | reslen = strftime(buff, SIZETIMEFMT, cc, stm);
315 | luaL_addsize(&b, reslen);
316 | }
317 | }
318 | luaL_pushresult(&b);
319 | }
320 | return 1;
321 | }
322 |
323 |
324 | static int os_time (lua_State *L) {
325 | time_t t;
326 | if (lua_isnoneornil(L, 1)) /* called without args? */
327 | t = time(NULL); /* get current time */
328 | else {
329 | struct tm ts;
330 | luaL_checktype(L, 1, LUA_TTABLE);
331 | lua_settop(L, 1); /* make sure table is at the top */
332 | ts.tm_sec = getfield(L, "sec", 0, 0);
333 | ts.tm_min = getfield(L, "min", 0, 0);
334 | ts.tm_hour = getfield(L, "hour", 12, 0);
335 | ts.tm_mday = getfield(L, "day", -1, 0);
336 | ts.tm_mon = getfield(L, "month", -1, 1);
337 | ts.tm_year = getfield(L, "year", -1, 1900);
338 | ts.tm_isdst = getboolfield(L, "isdst");
339 | t = mktime(&ts);
340 | setallfields(L, &ts); /* update fields with normalized values */
341 | }
342 | if (t != (time_t)(l_timet)t || t == (time_t)(-1))
343 | luaL_error(L, "time result cannot be represented in this installation");
344 | l_pushtime(L, t);
345 | return 1;
346 | }
347 |
348 |
349 | static int os_difftime (lua_State *L) {
350 | time_t t1 = l_checktime(L, 1);
351 | time_t t2 = l_checktime(L, 2);
352 | lua_pushnumber(L, (lua_Number)difftime(t1, t2));
353 | return 1;
354 | }
355 |
356 | /* }====================================================== */
357 |
358 |
359 | static int os_setlocale (lua_State *L) {
360 | static const int cat[] = {LC_ALL, LC_COLLATE, LC_CTYPE, LC_MONETARY,
361 | LC_NUMERIC, LC_TIME};
362 | static const char *const catnames[] = {"all", "collate", "ctype", "monetary",
363 | "numeric", "time", NULL};
364 | const char *l = luaL_optstring(L, 1, NULL);
365 | int op = luaL_checkoption(L, 2, "all", catnames);
366 | lua_pushstring(L, setlocale(cat[op], l));
367 | return 1;
368 | }
369 |
370 |
371 | static int os_exit (lua_State *L) {
372 | int status;
373 | if (lua_isboolean(L, 1))
374 | status = (lua_toboolean(L, 1) ? EXIT_SUCCESS : EXIT_FAILURE);
375 | else
376 | status = (int)luaL_optinteger(L, 1, EXIT_SUCCESS);
377 | if (lua_toboolean(L, 2))
378 | lua_close(L);
379 | if (L) exit(status); /* 'if' to avoid warnings for unreachable 'return' */
380 | return 0;
381 | }
382 |
383 |
384 | static const luaL_Reg syslib[] = {
385 | {"clock", os_clock},
386 | {"date", os_date},
387 | {"difftime", os_difftime},
388 | {"execute", os_execute},
389 | {"exit", os_exit},
390 | {"getenv", os_getenv},
391 | {"remove", os_remove},
392 | {"rename", os_rename},
393 | {"setlocale", os_setlocale},
394 | {"time", os_time},
395 | {"tmpname", os_tmpname},
396 | {NULL, NULL}
397 | };
398 |
399 | /* }====================================================== */
400 |
401 |
402 |
403 | LUAMOD_API int luaopen_os (lua_State *L) {
404 | luaL_newlib(L, syslib);
405 | return 1;
406 | }
407 |
408 |
--------------------------------------------------------------------------------
/lualib/src/main/cpp/lua/lparser.h:
--------------------------------------------------------------------------------
1 | /*
2 | ** $Id: lparser.h,v 1.76 2015/12/30 18:16:13 roberto Exp $
3 | ** Lua Parser
4 | ** See Copyright Notice in lua.h
5 | */
6 |
7 | #ifndef lparser_h
8 | #define lparser_h
9 |
10 | #include "llimits.h"
11 | #include "lobject.h"
12 | #include "lzio.h"
13 |
14 |
15 | /*
16 | ** Expression and variable descriptor.
17 | ** Code generation for variables and expressions can be delayed to allow
18 | ** optimizations; An 'expdesc' structure describes a potentially-delayed
19 | ** variable/expression. It has a description of its "main" value plus a
20 | ** list of conditional jumps that can also produce its value (generated
21 | ** by short-circuit operators 'and'/'or').
22 | */
23 |
24 | /* kinds of variables/expressions */
25 | typedef enum {
26 | VVOID, /* when 'expdesc' describes the last expression a list,
27 | this kind means an empty list (so, no expression) */
28 | VNIL, /* constant nil */
29 | VTRUE, /* constant true */
30 | VFALSE, /* constant false */
31 | VK, /* constant in 'k'; info = index of constant in 'k' */
32 | VKFLT, /* floating constant; nval = numerical float value */
33 | VKINT, /* integer constant; nval = numerical integer value */
34 | VNONRELOC, /* expression has its value in a fixed register;
35 | info = result register */
36 | VLOCAL, /* local variable; info = local register */
37 | VUPVAL, /* upvalue variable; info = index of upvalue in 'upvalues' */
38 | VINDEXED, /* indexed variable;
39 | ind.vt = whether 't' is register or upvalue;
40 | ind.t = table register or upvalue;
41 | ind.idx = key's R/K index */
42 | VJMP, /* expression is a test/comparison;
43 | info = pc of corresponding jump instruction */
44 | VRELOCABLE, /* expression can put result in any register;
45 | info = instruction pc */
46 | VCALL, /* expression is a function call; info = instruction pc */
47 | VVARARG /* vararg expression; info = instruction pc */
48 | } expkind;
49 |
50 |
51 | #define vkisvar(k) (VLOCAL <= (k) && (k) <= VINDEXED)
52 | #define vkisinreg(k) ((k) == VNONRELOC || (k) == VLOCAL)
53 |
54 | typedef struct expdesc {
55 | expkind k;
56 | union {
57 | lua_Integer ival; /* for VKINT */
58 | lua_Number nval; /* for VKFLT */
59 | int info; /* for generic use */
60 | struct { /* for indexed variables (VINDEXED) */
61 | short idx; /* index (R/K) */
62 | lu_byte t; /* table (register or upvalue) */
63 | lu_byte vt; /* whether 't' is register (VLOCAL) or upvalue (VUPVAL) */
64 | } ind;
65 | } u;
66 | int t; /* patch list of 'exit when true' */
67 | int f; /* patch list of 'exit when false' */
68 | } expdesc;
69 |
70 |
71 | /* description of active local variable */
72 | typedef struct Vardesc {
73 | short idx; /* variable index in stack */
74 | } Vardesc;
75 |
76 |
77 | /* description of pending goto statements and label statements */
78 | typedef struct Labeldesc {
79 | TString *name; /* label identifier */
80 | int pc; /* position in code */
81 | int line; /* line where it appeared */
82 | lu_byte nactvar; /* local level where it appears in current block */
83 | } Labeldesc;
84 |
85 |
86 | /* list of labels or gotos */
87 | typedef struct Labellist {
88 | Labeldesc *arr; /* array */
89 | int n; /* number of entries in use */
90 | int size; /* array size */
91 | } Labellist;
92 |
93 |
94 | /* dynamic structures used by the parser */
95 | typedef struct Dyndata {
96 | struct { /* list of active local variables */
97 | Vardesc *arr;
98 | int n;
99 | int size;
100 | } actvar;
101 | Labellist gt; /* list of pending gotos */
102 | Labellist label; /* list of active labels */
103 | } Dyndata;
104 |
105 |
106 | /* control of blocks */
107 | struct BlockCnt; /* defined in lparser.c */
108 |
109 |
110 | /* state needed to generate code for a given function */
111 | typedef struct FuncState {
112 | Proto *f; /* current function header */
113 | struct FuncState *prev; /* enclosing function */
114 | struct LexState *ls; /* lexical state */
115 | struct BlockCnt *bl; /* chain of current blocks */
116 | int pc; /* next position to code (equivalent to 'ncode') */
117 | int lasttarget; /* 'label' of last 'jump label' */
118 | int jpc; /* list of pending jumps to 'pc' */
119 | int nk; /* number of elements in 'k' */
120 | int np; /* number of elements in 'p' */
121 | int firstlocal; /* index of first local var (in Dyndata array) */
122 | short nlocvars; /* number of elements in 'f->locvars' */
123 | lu_byte nactvar; /* number of active local variables */
124 | lu_byte nups; /* number of upvalues */
125 | lu_byte freereg; /* first free register */
126 | } FuncState;
127 |
128 |
129 | LUAI_FUNC LClosure *luaY_parser (lua_State *L, ZIO *z, Mbuffer *buff,
130 | Dyndata *dyd, const char *name, int firstchar);
131 |
132 |
133 | #endif
134 |
--------------------------------------------------------------------------------
/lualib/src/main/cpp/lua/lprefix.h:
--------------------------------------------------------------------------------
1 | /*
2 | ** $Id: lprefix.h,v 1.2 2014/12/29 16:54:13 roberto Exp $
3 | ** Definitions for Lua code that must come before any other header file
4 | ** See Copyright Notice in lua.h
5 | */
6 |
7 | #ifndef lprefix_h
8 | #define lprefix_h
9 |
10 |
11 | /*
12 | ** Allows POSIX/XSI stuff
13 | */
14 | #if !defined(LUA_USE_C89) /* { */
15 |
16 | #if !defined(_XOPEN_SOURCE)
17 | #define _XOPEN_SOURCE 600
18 | #elif _XOPEN_SOURCE == 0
19 | #undef _XOPEN_SOURCE /* use -D_XOPEN_SOURCE=0 to undefine it */
20 | #endif
21 |
22 | /*
23 | ** Allows manipulation of large files in gcc and some other compilers
24 | */
25 | #if !defined(LUA_32BITS) && !defined(_FILE_OFFSET_BITS)
26 | #define _LARGEFILE_SOURCE 1
27 | #define _FILE_OFFSET_BITS 64
28 | #endif
29 |
30 | #endif /* } */
31 |
32 |
33 | /*
34 | ** Windows stuff
35 | */
36 | #if defined(_WIN32) /* { */
37 |
38 | #if !defined(_CRT_SECURE_NO_WARNINGS)
39 | #define _CRT_SECURE_NO_WARNINGS /* avoid warnings about ISO C functions */
40 | #endif
41 |
42 | #endif /* } */
43 |
44 | #endif
45 |
46 |
--------------------------------------------------------------------------------
/lualib/src/main/cpp/lua/lstate.c:
--------------------------------------------------------------------------------
1 | /*
2 | ** $Id: lstate.c,v 2.133 2015/11/13 12:16:51 roberto Exp $
3 | ** Global State
4 | ** See Copyright Notice in lua.h
5 | */
6 |
7 | #define lstate_c
8 | #define LUA_CORE
9 |
10 | #include "lprefix.h"
11 |
12 |
13 | #include
14 | #include
15 |
16 | #include "lua.h"
17 |
18 | #include "lapi.h"
19 | #include "ldebug.h"
20 | #include "ldo.h"
21 | #include "lfunc.h"
22 | #include "lgc.h"
23 | #include "llex.h"
24 | #include "lmem.h"
25 | #include "lstate.h"
26 | #include "lstring.h"
27 | #include "ltable.h"
28 | #include "ltm.h"
29 |
30 |
31 | #if !defined(LUAI_GCPAUSE)
32 | #define LUAI_GCPAUSE 200 /* 200% */
33 | #endif
34 |
35 | #if !defined(LUAI_GCMUL)
36 | #define LUAI_GCMUL 200 /* GC runs 'twice the speed' of memory allocation */
37 | #endif
38 |
39 |
40 | /*
41 | ** a macro to help the creation of a unique random seed when a state is
42 | ** created; the seed is used to randomize hashes.
43 | */
44 | #if !defined(luai_makeseed)
45 | #include
46 | #define luai_makeseed() cast(unsigned int, time(NULL))
47 | #endif
48 |
49 |
50 |
51 | /*
52 | ** thread state + extra space
53 | */
54 | typedef struct LX {
55 | lu_byte extra_[LUA_EXTRASPACE];
56 | lua_State l;
57 | } LX;
58 |
59 |
60 | /*
61 | ** Main thread combines a thread state and the global state
62 | */
63 | typedef struct LG {
64 | LX l;
65 | global_State g;
66 | } LG;
67 |
68 |
69 |
70 | #define fromstate(L) (cast(LX *, cast(lu_byte *, (L)) - offsetof(LX, l)))
71 |
72 |
73 | /*
74 | ** Compute an initial seed as random as possible. Rely on Address Space
75 | ** Layout Randomization (if present) to increase randomness..
76 | */
77 | #define addbuff(b,p,e) \
78 | { size_t t = cast(size_t, e); \
79 | memcpy(b + p, &t, sizeof(t)); p += sizeof(t); }
80 |
81 | static unsigned int makeseed (lua_State *L) {
82 | char buff[4 * sizeof(size_t)];
83 | unsigned int h = luai_makeseed();
84 | int p = 0;
85 | addbuff(buff, p, L); /* heap variable */
86 | addbuff(buff, p, &h); /* local variable */
87 | addbuff(buff, p, luaO_nilobject); /* global variable */
88 | addbuff(buff, p, &lua_newstate); /* public function */
89 | lua_assert(p == sizeof(buff));
90 | return luaS_hash(buff, p, h);
91 | }
92 |
93 |
94 | /*
95 | ** set GCdebt to a new value keeping the value (totalbytes + GCdebt)
96 | ** invariant (and avoiding underflows in 'totalbytes')
97 | */
98 | void luaE_setdebt (global_State *g, l_mem debt) {
99 | l_mem tb = gettotalbytes(g);
100 | lua_assert(tb > 0);
101 | if (debt < tb - MAX_LMEM)
102 | debt = tb - MAX_LMEM; /* will make 'totalbytes == MAX_LMEM' */
103 | g->totalbytes = tb - debt;
104 | g->GCdebt = debt;
105 | }
106 |
107 |
108 | CallInfo *luaE_extendCI (lua_State *L) {
109 | CallInfo *ci = luaM_new(L, CallInfo);
110 | lua_assert(L->ci->next == NULL);
111 | L->ci->next = ci;
112 | ci->previous = L->ci;
113 | ci->next = NULL;
114 | L->nci++;
115 | return ci;
116 | }
117 |
118 |
119 | /*
120 | ** free all CallInfo structures not in use by a thread
121 | */
122 | void luaE_freeCI (lua_State *L) {
123 | CallInfo *ci = L->ci;
124 | CallInfo *next = ci->next;
125 | ci->next = NULL;
126 | while ((ci = next) != NULL) {
127 | next = ci->next;
128 | luaM_free(L, ci);
129 | L->nci--;
130 | }
131 | }
132 |
133 |
134 | /*
135 | ** free half of the CallInfo structures not in use by a thread
136 | */
137 | void luaE_shrinkCI (lua_State *L) {
138 | CallInfo *ci = L->ci;
139 | CallInfo *next2; /* next's next */
140 | /* while there are two nexts */
141 | while (ci->next != NULL && (next2 = ci->next->next) != NULL) {
142 | luaM_free(L, ci->next); /* free next */
143 | L->nci--;
144 | ci->next = next2; /* remove 'next' from the list */
145 | next2->previous = ci;
146 | ci = next2; /* keep next's next */
147 | }
148 | }
149 |
150 |
151 | static void stack_init (lua_State *L1, lua_State *L) {
152 | int i; CallInfo *ci;
153 | /* initialize stack array */
154 | L1->stack = luaM_newvector(L, BASIC_STACK_SIZE, TValue);
155 | L1->stacksize = BASIC_STACK_SIZE;
156 | for (i = 0; i < BASIC_STACK_SIZE; i++)
157 | setnilvalue(L1->stack + i); /* erase new stack */
158 | L1->top = L1->stack;
159 | L1->stack_last = L1->stack + L1->stacksize - EXTRA_STACK;
160 | /* initialize first ci */
161 | ci = &L1->base_ci;
162 | ci->next = ci->previous = NULL;
163 | ci->callstatus = 0;
164 | ci->func = L1->top;
165 | setnilvalue(L1->top++); /* 'function' entry for this 'ci' */
166 | ci->top = L1->top + LUA_MINSTACK;
167 | L1->ci = ci;
168 | }
169 |
170 |
171 | static void freestack (lua_State *L) {
172 | if (L->stack == NULL)
173 | return; /* stack not completely built yet */
174 | L->ci = &L->base_ci; /* free the entire 'ci' list */
175 | luaE_freeCI(L);
176 | lua_assert(L->nci == 0);
177 | luaM_freearray(L, L->stack, L->stacksize); /* free stack array */
178 | }
179 |
180 |
181 | /*
182 | ** Create registry table and its predefined values
183 | */
184 | static void init_registry (lua_State *L, global_State *g) {
185 | TValue temp;
186 | /* create registry */
187 | Table *registry = luaH_new(L);
188 | sethvalue(L, &g->l_registry, registry);
189 | luaH_resize(L, registry, LUA_RIDX_LAST, 0);
190 | /* registry[LUA_RIDX_MAINTHREAD] = L */
191 | setthvalue(L, &temp, L); /* temp = L */
192 | luaH_setint(L, registry, LUA_RIDX_MAINTHREAD, &temp);
193 | /* registry[LUA_RIDX_GLOBALS] = table of globals */
194 | sethvalue(L, &temp, luaH_new(L)); /* temp = new table (global table) */
195 | luaH_setint(L, registry, LUA_RIDX_GLOBALS, &temp);
196 | }
197 |
198 |
199 | /*
200 | ** open parts of the state that may cause memory-allocation errors.
201 | ** ('g->version' != NULL flags that the state was completely build)
202 | */
203 | static void f_luaopen (lua_State *L, void *ud) {
204 | global_State *g = G(L);
205 | UNUSED(ud);
206 | stack_init(L, L); /* init stack */
207 | init_registry(L, g);
208 | luaS_init(L);
209 | luaT_init(L);
210 | luaX_init(L);
211 | g->gcrunning = 1; /* allow gc */
212 | g->version = lua_version(NULL);
213 | luai_userstateopen(L);
214 | }
215 |
216 |
217 | /*
218 | ** preinitialize a thread with consistent values without allocating
219 | ** any memory (to avoid errors)
220 | */
221 | static void preinit_thread (lua_State *L, global_State *g) {
222 | G(L) = g;
223 | L->stack = NULL;
224 | L->ci = NULL;
225 | L->nci = 0;
226 | L->stacksize = 0;
227 | L->twups = L; /* thread has no upvalues */
228 | L->errorJmp = NULL;
229 | L->nCcalls = 0;
230 | L->hook = NULL;
231 | L->hookmask = 0;
232 | L->basehookcount = 0;
233 | L->allowhook = 1;
234 | resethookcount(L);
235 | L->openupval = NULL;
236 | L->nny = 1;
237 | L->status = LUA_OK;
238 | L->errfunc = 0;
239 | }
240 |
241 |
242 | static void close_state (lua_State *L) {
243 | global_State *g = G(L);
244 | luaF_close(L, L->stack); /* close all upvalues for this thread */
245 | luaC_freeallobjects(L); /* collect all objects */
246 | if (g->version) /* closing a fully built state? */
247 | luai_userstateclose(L);
248 | luaM_freearray(L, G(L)->strt.hash, G(L)->strt.size);
249 | freestack(L);
250 | lua_assert(gettotalbytes(g) == sizeof(LG));
251 | (*g->frealloc)(g->ud, fromstate(L), sizeof(LG), 0); /* free main block */
252 | }
253 |
254 |
255 | LUA_API lua_State *lua_newthread (lua_State *L) {
256 | global_State *g = G(L);
257 | lua_State *L1;
258 | lua_lock(L);
259 | luaC_checkGC(L);
260 | /* create new thread */
261 | L1 = &cast(LX *, luaM_newobject(L, LUA_TTHREAD, sizeof(LX)))->l;
262 | L1->marked = luaC_white(g);
263 | L1->tt = LUA_TTHREAD;
264 | /* link it on list 'allgc' */
265 | L1->next = g->allgc;
266 | g->allgc = obj2gco(L1);
267 | /* anchor it on L stack */
268 | setthvalue(L, L->top, L1);
269 | api_incr_top(L);
270 | preinit_thread(L1, g);
271 | L1->hookmask = L->hookmask;
272 | L1->basehookcount = L->basehookcount;
273 | L1->hook = L->hook;
274 | resethookcount(L1);
275 | /* initialize L1 extra space */
276 | memcpy(lua_getextraspace(L1), lua_getextraspace(g->mainthread),
277 | LUA_EXTRASPACE);
278 | luai_userstatethread(L, L1);
279 | stack_init(L1, L); /* init stack */
280 | lua_unlock(L);
281 | return L1;
282 | }
283 |
284 |
285 | void luaE_freethread (lua_State *L, lua_State *L1) {
286 | LX *l = fromstate(L1);
287 | luaF_close(L1, L1->stack); /* close all upvalues for this thread */
288 | lua_assert(L1->openupval == NULL);
289 | luai_userstatefree(L, L1);
290 | freestack(L1);
291 | luaM_free(L, l);
292 | }
293 |
294 |
295 | LUA_API lua_State *lua_newstate (lua_Alloc f, void *ud) {
296 | int i;
297 | lua_State *L;
298 | global_State *g;
299 | LG *l = cast(LG *, (*f)(ud, NULL, LUA_TTHREAD, sizeof(LG)));
300 | if (l == NULL) return NULL;
301 | L = &l->l.l;
302 | g = &l->g;
303 | L->next = NULL;
304 | L->tt = LUA_TTHREAD;
305 | g->currentwhite = bitmask(WHITE0BIT);
306 | L->marked = luaC_white(g);
307 | preinit_thread(L, g);
308 | g->frealloc = f;
309 | g->ud = ud;
310 | g->mainthread = L;
311 | g->seed = makeseed(L);
312 | g->gcrunning = 0; /* no GC while building state */
313 | g->GCestimate = 0;
314 | g->strt.size = g->strt.nuse = 0;
315 | g->strt.hash = NULL;
316 | setnilvalue(&g->l_registry);
317 | g->panic = NULL;
318 | g->version = NULL;
319 | g->gcstate = GCSpause;
320 | g->gckind = KGC_NORMAL;
321 | g->allgc = g->finobj = g->tobefnz = g->fixedgc = NULL;
322 | g->sweepgc = NULL;
323 | g->gray = g->grayagain = NULL;
324 | g->weak = g->ephemeron = g->allweak = NULL;
325 | g->twups = NULL;
326 | g->totalbytes = sizeof(LG);
327 | g->GCdebt = 0;
328 | g->gcfinnum = 0;
329 | g->gcpause = LUAI_GCPAUSE;
330 | g->gcstepmul = LUAI_GCMUL;
331 | for (i=0; i < LUA_NUMTAGS; i++) g->mt[i] = NULL;
332 | if (luaD_rawrunprotected(L, f_luaopen, NULL) != LUA_OK) {
333 | /* memory allocation error: free partial state */
334 | close_state(L);
335 | L = NULL;
336 | }
337 | return L;
338 | }
339 |
340 |
341 | LUA_API void lua_close (lua_State *L) {
342 | L = G(L)->mainthread; /* only the main thread can be closed */
343 | lua_lock(L);
344 | close_state(L);
345 | }
346 |
347 |
348 |
--------------------------------------------------------------------------------
/lualib/src/main/cpp/lua/lstate.h:
--------------------------------------------------------------------------------
1 | /*
2 | ** $Id: lstate.h,v 2.133 2016/12/22 13:08:50 roberto Exp $
3 | ** Global State
4 | ** See Copyright Notice in lua.h
5 | */
6 |
7 | #ifndef lstate_h
8 | #define lstate_h
9 |
10 | #include "lua.h"
11 |
12 | #include "lobject.h"
13 | #include "ltm.h"
14 | #include "lzio.h"
15 |
16 |
17 | /*
18 |
19 | ** Some notes about garbage-collected objects: All objects in Lua must
20 | ** be kept somehow accessible until being freed, so all objects always
21 | ** belong to one (and only one) of these lists, using field 'next' of
22 | ** the 'CommonHeader' for the link:
23 | **
24 | ** 'allgc': all objects not marked for finalization;
25 | ** 'finobj': all objects marked for finalization;
26 | ** 'tobefnz': all objects ready to be finalized;
27 | ** 'fixedgc': all objects that are not to be collected (currently
28 | ** only small strings, such as reserved words).
29 |
30 | */
31 |
32 |
33 | struct lua_longjmp; /* defined in ldo.c */
34 |
35 |
36 | /*
37 | ** Atomic type (relative to signals) to better ensure that 'lua_sethook'
38 | ** is thread safe
39 | */
40 | #if !defined(l_signalT)
41 | #include
42 | #define l_signalT sig_atomic_t
43 | #endif
44 |
45 |
46 | /* extra stack space to handle TM calls and some other extras */
47 | #define EXTRA_STACK 5
48 |
49 |
50 | #define BASIC_STACK_SIZE (2*LUA_MINSTACK)
51 |
52 |
53 | /* kinds of Garbage Collection */
54 | #define KGC_NORMAL 0
55 | #define KGC_EMERGENCY 1 /* gc was forced by an allocation failure */
56 |
57 |
58 | typedef struct stringtable {
59 | TString **hash;
60 | int nuse; /* number of elements */
61 | int size;
62 | } stringtable;
63 |
64 |
65 | /*
66 | ** Information about a call.
67 | ** When a thread yields, 'func' is adjusted to pretend that the
68 | ** top function has only the yielded values in its stack; in that
69 | ** case, the actual 'func' value is saved in field 'extra'.
70 | ** When a function calls another with a continuation, 'extra' keeps
71 | ** the function index so that, in case of errors, the continuation
72 | ** function can be called with the correct top.
73 | */
74 | typedef struct CallInfo {
75 | StkId func; /* function index in the stack */
76 | StkId top; /* top for this function */
77 | struct CallInfo *previous, *next; /* dynamic call link */
78 | union {
79 | struct { /* only for Lua functions */
80 | StkId base; /* base for this function */
81 | const Instruction *savedpc;
82 | } l;
83 | struct { /* only for C functions */
84 | lua_KFunction k; /* continuation in case of yields */
85 | ptrdiff_t old_errfunc;
86 | lua_KContext ctx; /* context info. in case of yields */
87 | } c;
88 | } u;
89 | ptrdiff_t extra;
90 | short nresults; /* expected number of results from this function */
91 | unsigned short callstatus;
92 | } CallInfo;
93 |
94 |
95 | /*
96 | ** Bits in CallInfo status
97 | */
98 | #define CIST_OAH (1<<0) /* original value of 'allowhook' */
99 | #define CIST_LUA (1<<1) /* call is running a Lua function */
100 | #define CIST_HOOKED (1<<2) /* call is running a debug hook */
101 | #define CIST_FRESH (1<<3) /* call is running on a fresh invocation
102 | of luaV_execute */
103 | #define CIST_YPCALL (1<<4) /* call is a yieldable protected call */
104 | #define CIST_TAIL (1<<5) /* call was tail called */
105 | #define CIST_HOOKYIELD (1<<6) /* last hook called yielded */
106 | #define CIST_LEQ (1<<7) /* using __lt for __le */
107 | #define CIST_FIN (1<<8) /* call is running a finalizer */
108 |
109 | #define isLua(ci) ((ci)->callstatus & CIST_LUA)
110 |
111 | /* assume that CIST_OAH has offset 0 and that 'v' is strictly 0/1 */
112 | #define setoah(st,v) ((st) = ((st) & ~CIST_OAH) | (v))
113 | #define getoah(st) ((st) & CIST_OAH)
114 |
115 |
116 | /*
117 | ** 'global state', shared by all threads of this state
118 | */
119 | typedef struct global_State {
120 | lua_Alloc frealloc; /* function to reallocate memory */
121 | void *ud; /* auxiliary data to 'frealloc' */
122 | l_mem totalbytes; /* number of bytes currently allocated - GCdebt */
123 | l_mem GCdebt; /* bytes allocated not yet compensated by the collector */
124 | lu_mem GCmemtrav; /* memory traversed by the GC */
125 | lu_mem GCestimate; /* an estimate of the non-garbage memory in use */
126 | stringtable strt; /* hash table for strings */
127 | TValue l_registry;
128 | unsigned int seed; /* randomized seed for hashes */
129 | lu_byte currentwhite;
130 | lu_byte gcstate; /* state of garbage collector */
131 | lu_byte gckind; /* kind of GC running */
132 | lu_byte gcrunning; /* true if GC is running */
133 | GCObject *allgc; /* list of all collectable objects */
134 | GCObject **sweepgc; /* current position of sweep in list */
135 | GCObject *finobj; /* list of collectable objects with finalizers */
136 | GCObject *gray; /* list of gray objects */
137 | GCObject *grayagain; /* list of objects to be traversed atomically */
138 | GCObject *weak; /* list of tables with weak values */
139 | GCObject *ephemeron; /* list of ephemeron tables (weak keys) */
140 | GCObject *allweak; /* list of all-weak tables */
141 | GCObject *tobefnz; /* list of userdata to be GC */
142 | GCObject *fixedgc; /* list of objects not to be collected */
143 | struct lua_State *twups; /* list of threads with open upvalues */
144 | unsigned int gcfinnum; /* number of finalizers to call in each GC step */
145 | int gcpause; /* size of pause between successive GCs */
146 | int gcstepmul; /* GC 'granularity' */
147 | lua_CFunction panic; /* to be called in unprotected errors */
148 | struct lua_State *mainthread;
149 | const lua_Number *version; /* pointer to version number */
150 | TString *memerrmsg; /* memory-error message */
151 | TString *tmname[TM_N]; /* array with tag-method names */
152 | struct Table *mt[LUA_NUMTAGS]; /* metatables for basic types */
153 | TString *strcache[STRCACHE_N][STRCACHE_M]; /* cache for strings in API */
154 | } global_State;
155 |
156 |
157 | /*
158 | ** 'per thread' state
159 | */
160 | struct lua_State {
161 | CommonHeader;
162 | unsigned short nci; /* number of items in 'ci' list */
163 | lu_byte status;
164 | StkId top; /* first free slot in the stack */
165 | global_State *l_G;
166 | CallInfo *ci; /* call info for current function */
167 | const Instruction *oldpc; /* last pc traced */
168 | StkId stack_last; /* last free slot in the stack */
169 | StkId stack; /* stack base */
170 | UpVal *openupval; /* list of open upvalues in this stack */
171 | GCObject *gclist;
172 | struct lua_State *twups; /* list of threads with open upvalues */
173 | struct lua_longjmp *errorJmp; /* current error recover point */
174 | CallInfo base_ci; /* CallInfo for first level (C calling Lua) */
175 | volatile lua_Hook hook;
176 | ptrdiff_t errfunc; /* current error handling function (stack index) */
177 | int stacksize;
178 | int basehookcount;
179 | int hookcount;
180 | unsigned short nny; /* number of non-yieldable calls in stack */
181 | unsigned short nCcalls; /* number of nested C calls */
182 | l_signalT hookmask;
183 | lu_byte allowhook;
184 | };
185 |
186 |
187 | #define G(L) (L->l_G)
188 |
189 |
190 | /*
191 | ** Union of all collectable objects (only for conversions)
192 | */
193 | union GCUnion {
194 | GCObject gc; /* common header */
195 | struct TString ts;
196 | struct Udata u;
197 | union Closure cl;
198 | struct Table h;
199 | struct Proto p;
200 | struct lua_State th; /* thread */
201 | };
202 |
203 |
204 | #define cast_u(o) cast(union GCUnion *, (o))
205 |
206 | /* macros to convert a GCObject into a specific value */
207 | #define gco2ts(o) \
208 | check_exp(novariant((o)->tt) == LUA_TSTRING, &((cast_u(o))->ts))
209 | #define gco2u(o) check_exp((o)->tt == LUA_TUSERDATA, &((cast_u(o))->u))
210 | #define gco2lcl(o) check_exp((o)->tt == LUA_TLCL, &((cast_u(o))->cl.l))
211 | #define gco2ccl(o) check_exp((o)->tt == LUA_TCCL, &((cast_u(o))->cl.c))
212 | #define gco2cl(o) \
213 | check_exp(novariant((o)->tt) == LUA_TFUNCTION, &((cast_u(o))->cl))
214 | #define gco2t(o) check_exp((o)->tt == LUA_TTABLE, &((cast_u(o))->h))
215 | #define gco2p(o) check_exp((o)->tt == LUA_TPROTO, &((cast_u(o))->p))
216 | #define gco2th(o) check_exp((o)->tt == LUA_TTHREAD, &((cast_u(o))->th))
217 |
218 |
219 | /* macro to convert a Lua object into a GCObject */
220 | #define obj2gco(v) \
221 | check_exp(novariant((v)->tt) < LUA_TDEADKEY, (&(cast_u(v)->gc)))
222 |
223 |
224 | /* actual number of total bytes allocated */
225 | #define gettotalbytes(g) cast(lu_mem, (g)->totalbytes + (g)->GCdebt)
226 |
227 | LUAI_FUNC void luaE_setdebt (global_State *g, l_mem debt);
228 | LUAI_FUNC void luaE_freethread (lua_State *L, lua_State *L1);
229 | LUAI_FUNC CallInfo *luaE_extendCI (lua_State *L);
230 | LUAI_FUNC void luaE_freeCI (lua_State *L);
231 | LUAI_FUNC void luaE_shrinkCI (lua_State *L);
232 |
233 |
234 | #endif
235 |
236 |
--------------------------------------------------------------------------------
/lualib/src/main/cpp/lua/lstring.c:
--------------------------------------------------------------------------------
1 | /*
2 | ** $Id: lstring.c,v 2.56 2015/11/23 11:32:51 roberto Exp $
3 | ** String table (keeps all strings handled by Lua)
4 | ** See Copyright Notice in lua.h
5 | */
6 |
7 | #define lstring_c
8 | #define LUA_CORE
9 |
10 | #include "lprefix.h"
11 |
12 |
13 | #include
14 |
15 | #include "lua.h"
16 |
17 | #include "ldebug.h"
18 | #include "ldo.h"
19 | #include "lmem.h"
20 | #include "lobject.h"
21 | #include "lstate.h"
22 | #include "lstring.h"
23 |
24 |
25 | #define MEMERRMSG "not enough memory"
26 |
27 |
28 | /*
29 | ** Lua will use at most ~(2^LUAI_HASHLIMIT) bytes from a string to
30 | ** compute its hash
31 | */
32 | #if !defined(LUAI_HASHLIMIT)
33 | #define LUAI_HASHLIMIT 5
34 | #endif
35 |
36 |
37 | /*
38 | ** equality for long strings
39 | */
40 | int luaS_eqlngstr (TString *a, TString *b) {
41 | size_t len = a->u.lnglen;
42 | lua_assert(a->tt == LUA_TLNGSTR && b->tt == LUA_TLNGSTR);
43 | return (a == b) || /* same instance or... */
44 | ((len == b->u.lnglen) && /* equal length and ... */
45 | (memcmp(getstr(a), getstr(b), len) == 0)); /* equal contents */
46 | }
47 |
48 |
49 | unsigned int luaS_hash (const char *str, size_t l, unsigned int seed) {
50 | unsigned int h = seed ^ cast(unsigned int, l);
51 | size_t step = (l >> LUAI_HASHLIMIT) + 1;
52 | for (; l >= step; l -= step)
53 | h ^= ((h<<5) + (h>>2) + cast_byte(str[l - 1]));
54 | return h;
55 | }
56 |
57 |
58 | unsigned int luaS_hashlongstr (TString *ts) {
59 | lua_assert(ts->tt == LUA_TLNGSTR);
60 | if (ts->extra == 0) { /* no hash? */
61 | ts->hash = luaS_hash(getstr(ts), ts->u.lnglen, ts->hash);
62 | ts->extra = 1; /* now it has its hash */
63 | }
64 | return ts->hash;
65 | }
66 |
67 |
68 | /*
69 | ** resizes the string table
70 | */
71 | void luaS_resize (lua_State *L, int newsize) {
72 | int i;
73 | stringtable *tb = &G(L)->strt;
74 | if (newsize > tb->size) { /* grow table if needed */
75 | luaM_reallocvector(L, tb->hash, tb->size, newsize, TString *);
76 | for (i = tb->size; i < newsize; i++)
77 | tb->hash[i] = NULL;
78 | }
79 | for (i = 0; i < tb->size; i++) { /* rehash */
80 | TString *p = tb->hash[i];
81 | tb->hash[i] = NULL;
82 | while (p) { /* for each node in the list */
83 | TString *hnext = p->u.hnext; /* save next */
84 | unsigned int h = lmod(p->hash, newsize); /* new position */
85 | p->u.hnext = tb->hash[h]; /* chain it */
86 | tb->hash[h] = p;
87 | p = hnext;
88 | }
89 | }
90 | if (newsize < tb->size) { /* shrink table if needed */
91 | /* vanishing slice should be empty */
92 | lua_assert(tb->hash[newsize] == NULL && tb->hash[tb->size - 1] == NULL);
93 | luaM_reallocvector(L, tb->hash, tb->size, newsize, TString *);
94 | }
95 | tb->size = newsize;
96 | }
97 |
98 |
99 | /*
100 | ** Clear API string cache. (Entries cannot be empty, so fill them with
101 | ** a non-collectable string.)
102 | */
103 | void luaS_clearcache (global_State *g) {
104 | int i, j;
105 | for (i = 0; i < STRCACHE_N; i++)
106 | for (j = 0; j < STRCACHE_M; j++) {
107 | if (iswhite(g->strcache[i][j])) /* will entry be collected? */
108 | g->strcache[i][j] = g->memerrmsg; /* replace it with something fixed */
109 | }
110 | }
111 |
112 |
113 | /*
114 | ** Initialize the string table and the string cache
115 | */
116 | void luaS_init (lua_State *L) {
117 | global_State *g = G(L);
118 | int i, j;
119 | luaS_resize(L, MINSTRTABSIZE); /* initial size of string table */
120 | /* pre-create memory-error message */
121 | g->memerrmsg = luaS_newliteral(L, MEMERRMSG);
122 | luaC_fix(L, obj2gco(g->memerrmsg)); /* it should never be collected */
123 | for (i = 0; i < STRCACHE_N; i++) /* fill cache with valid strings */
124 | for (j = 0; j < STRCACHE_M; j++)
125 | g->strcache[i][j] = g->memerrmsg;
126 | }
127 |
128 |
129 |
130 | /*
131 | ** creates a new string object
132 | */
133 | static TString *createstrobj (lua_State *L, size_t l, int tag, unsigned int h) {
134 | TString *ts;
135 | GCObject *o;
136 | size_t totalsize; /* total size of TString object */
137 | totalsize = sizelstring(l);
138 | o = luaC_newobj(L, tag, totalsize);
139 | ts = gco2ts(o);
140 | ts->hash = h;
141 | ts->extra = 0;
142 | getstr(ts)[l] = '\0'; /* ending 0 */
143 | return ts;
144 | }
145 |
146 |
147 | TString *luaS_createlngstrobj (lua_State *L, size_t l) {
148 | TString *ts = createstrobj(L, l, LUA_TLNGSTR, G(L)->seed);
149 | ts->u.lnglen = l;
150 | return ts;
151 | }
152 |
153 |
154 | void luaS_remove (lua_State *L, TString *ts) {
155 | stringtable *tb = &G(L)->strt;
156 | TString **p = &tb->hash[lmod(ts->hash, tb->size)];
157 | while (*p != ts) /* find previous element */
158 | p = &(*p)->u.hnext;
159 | *p = (*p)->u.hnext; /* remove element from its list */
160 | tb->nuse--;
161 | }
162 |
163 |
164 | /*
165 | ** checks whether short string exists and reuses it or creates a new one
166 | */
167 | static TString *internshrstr (lua_State *L, const char *str, size_t l) {
168 | TString *ts;
169 | global_State *g = G(L);
170 | unsigned int h = luaS_hash(str, l, g->seed);
171 | TString **list = &g->strt.hash[lmod(h, g->strt.size)];
172 | lua_assert(str != NULL); /* otherwise 'memcmp'/'memcpy' are undefined */
173 | for (ts = *list; ts != NULL; ts = ts->u.hnext) {
174 | if (l == ts->shrlen &&
175 | (memcmp(str, getstr(ts), l * sizeof(char)) == 0)) {
176 | /* found! */
177 | if (isdead(g, ts)) /* dead (but not collected yet)? */
178 | changewhite(ts); /* resurrect it */
179 | return ts;
180 | }
181 | }
182 | if (g->strt.nuse >= g->strt.size && g->strt.size <= MAX_INT/2) {
183 | luaS_resize(L, g->strt.size * 2);
184 | list = &g->strt.hash[lmod(h, g->strt.size)]; /* recompute with new size */
185 | }
186 | ts = createstrobj(L, l, LUA_TSHRSTR, h);
187 | memcpy(getstr(ts), str, l * sizeof(char));
188 | ts->shrlen = cast_byte(l);
189 | ts->u.hnext = *list;
190 | *list = ts;
191 | g->strt.nuse++;
192 | return ts;
193 | }
194 |
195 |
196 | /*
197 | ** new string (with explicit length)
198 | */
199 | TString *luaS_newlstr (lua_State *L, const char *str, size_t l) {
200 | if (l <= LUAI_MAXSHORTLEN) /* short string? */
201 | return internshrstr(L, str, l);
202 | else {
203 | TString *ts;
204 | if (l >= (MAX_SIZE - sizeof(TString))/sizeof(char))
205 | luaM_toobig(L);
206 | ts = luaS_createlngstrobj(L, l);
207 | memcpy(getstr(ts), str, l * sizeof(char));
208 | return ts;
209 | }
210 | }
211 |
212 |
213 | /*
214 | ** Create or reuse a zero-terminated string, first checking in the
215 | ** cache (using the string address as a key). The cache can contain
216 | ** only zero-terminated strings, so it is safe to use 'strcmp' to
217 | ** check hits.
218 | */
219 | TString *luaS_new (lua_State *L, const char *str) {
220 | unsigned int i = point2uint(str) % STRCACHE_N; /* hash */
221 | int j;
222 | TString **p = G(L)->strcache[i];
223 | for (j = 0; j < STRCACHE_M; j++) {
224 | if (strcmp(str, getstr(p[j])) == 0) /* hit? */
225 | return p[j]; /* that is it */
226 | }
227 | /* normal route */
228 | for (j = STRCACHE_M - 1; j > 0; j--)
229 | p[j] = p[j - 1]; /* move out last element */
230 | /* new element is first in the list */
231 | p[0] = luaS_newlstr(L, str, strlen(str));
232 | return p[0];
233 | }
234 |
235 |
236 | Udata *luaS_newudata (lua_State *L, size_t s) {
237 | Udata *u;
238 | GCObject *o;
239 | if (s > MAX_SIZE - sizeof(Udata))
240 | luaM_toobig(L);
241 | o = luaC_newobj(L, LUA_TUSERDATA, sizeludata(s));
242 | u = gco2u(o);
243 | u->len = s;
244 | u->metatable = NULL;
245 | setuservalue(L, u, luaO_nilobject);
246 | return u;
247 | }
248 |
249 |
--------------------------------------------------------------------------------
/lualib/src/main/cpp/lua/lstring.h:
--------------------------------------------------------------------------------
1 | /*
2 | ** $Id: lstring.h,v 1.61 2015/11/03 15:36:01 roberto Exp $
3 | ** String table (keep all strings handled by Lua)
4 | ** See Copyright Notice in lua.h
5 | */
6 |
7 | #ifndef lstring_h
8 | #define lstring_h
9 |
10 | #include "lgc.h"
11 | #include "lobject.h"
12 | #include "lstate.h"
13 |
14 |
15 | #define sizelstring(l) (sizeof(union UTString) + ((l) + 1) * sizeof(char))
16 |
17 | #define sizeludata(l) (sizeof(union UUdata) + (l))
18 | #define sizeudata(u) sizeludata((u)->len)
19 |
20 | #define luaS_newliteral(L, s) (luaS_newlstr(L, "" s, \
21 | (sizeof(s)/sizeof(char))-1))
22 |
23 |
24 | /*
25 | ** test whether a string is a reserved word
26 | */
27 | #define isreserved(s) ((s)->tt == LUA_TSHRSTR && (s)->extra > 0)
28 |
29 |
30 | /*
31 | ** equality for short strings, which are always internalized
32 | */
33 | #define eqshrstr(a,b) check_exp((a)->tt == LUA_TSHRSTR, (a) == (b))
34 |
35 |
36 | LUAI_FUNC unsigned int luaS_hash (const char *str, size_t l, unsigned int seed);
37 | LUAI_FUNC unsigned int luaS_hashlongstr (TString *ts);
38 | LUAI_FUNC int luaS_eqlngstr (TString *a, TString *b);
39 | LUAI_FUNC void luaS_resize (lua_State *L, int newsize);
40 | LUAI_FUNC void luaS_clearcache (global_State *g);
41 | LUAI_FUNC void luaS_init (lua_State *L);
42 | LUAI_FUNC void luaS_remove (lua_State *L, TString *ts);
43 | LUAI_FUNC Udata *luaS_newudata (lua_State *L, size_t s);
44 | LUAI_FUNC TString *luaS_newlstr (lua_State *L, const char *str, size_t l);
45 | LUAI_FUNC TString *luaS_new (lua_State *L, const char *str);
46 | LUAI_FUNC TString *luaS_createlngstrobj (lua_State *L, size_t l);
47 |
48 |
49 | #endif
50 |
--------------------------------------------------------------------------------
/lualib/src/main/cpp/lua/ltable.h:
--------------------------------------------------------------------------------
1 | /*
2 | ** $Id: ltable.h,v 2.23 2016/12/22 13:08:50 roberto Exp $
3 | ** Lua tables (hash)
4 | ** See Copyright Notice in lua.h
5 | */
6 |
7 | #ifndef ltable_h
8 | #define ltable_h
9 |
10 | #include "lobject.h"
11 |
12 |
13 | #define gnode(t,i) (&(t)->node[i])
14 | #define gval(n) (&(n)->i_val)
15 | #define gnext(n) ((n)->i_key.nk.next)
16 |
17 |
18 | /* 'const' to avoid wrong writings that can mess up field 'next' */
19 | #define gkey(n) cast(const TValue*, (&(n)->i_key.tvk))
20 |
21 | /*
22 | ** writable version of 'gkey'; allows updates to individual fields,
23 | ** but not to the whole (which has incompatible type)
24 | */
25 | #define wgkey(n) (&(n)->i_key.nk)
26 |
27 | #define invalidateTMcache(t) ((t)->flags = 0)
28 |
29 |
30 | /* true when 't' is using 'dummynode' as its hash part */
31 | #define isdummy(t) ((t)->lastfree == NULL)
32 |
33 |
34 | /* allocated size for hash nodes */
35 | #define allocsizenode(t) (isdummy(t) ? 0 : sizenode(t))
36 |
37 |
38 | /* returns the key, given the value of a table entry */
39 | #define keyfromval(v) \
40 | (gkey(cast(Node *, cast(char *, (v)) - offsetof(Node, i_val))))
41 |
42 |
43 | LUAI_FUNC const TValue *luaH_getint (Table *t, lua_Integer key);
44 | LUAI_FUNC void luaH_setint (lua_State *L, Table *t, lua_Integer key,
45 | TValue *value);
46 | LUAI_FUNC const TValue *luaH_getshortstr (Table *t, TString *key);
47 | LUAI_FUNC const TValue *luaH_getstr (Table *t, TString *key);
48 | LUAI_FUNC const TValue *luaH_get (Table *t, const TValue *key);
49 | LUAI_FUNC TValue *luaH_newkey (lua_State *L, Table *t, const TValue *key);
50 | LUAI_FUNC TValue *luaH_set (lua_State *L, Table *t, const TValue *key);
51 | LUAI_FUNC Table *luaH_new (lua_State *L);
52 | LUAI_FUNC void luaH_resize (lua_State *L, Table *t, unsigned int nasize,
53 | unsigned int nhsize);
54 | LUAI_FUNC void luaH_resizearray (lua_State *L, Table *t, unsigned int nasize);
55 | LUAI_FUNC void luaH_free (lua_State *L, Table *t);
56 | LUAI_FUNC int luaH_next (lua_State *L, Table *t, StkId key);
57 | LUAI_FUNC int luaH_getn (Table *t);
58 |
59 |
60 | #if defined(LUA_DEBUG)
61 | LUAI_FUNC Node *luaH_mainposition (const Table *t, const TValue *key);
62 | LUAI_FUNC int luaH_isdummy (const Table *t);
63 | #endif
64 |
65 |
66 | #endif
67 |
--------------------------------------------------------------------------------
/lualib/src/main/cpp/lua/ltm.c:
--------------------------------------------------------------------------------
1 | /*
2 | ** $Id: ltm.c,v 2.38 2016/12/22 13:08:50 roberto Exp $
3 | ** Tag methods
4 | ** See Copyright Notice in lua.h
5 | */
6 |
7 | #define ltm_c
8 | #define LUA_CORE
9 |
10 | #include "lprefix.h"
11 |
12 |
13 | #include
14 |
15 | #include "lua.h"
16 |
17 | #include "ldebug.h"
18 | #include "ldo.h"
19 | #include "lobject.h"
20 | #include "lstate.h"
21 | #include "lstring.h"
22 | #include "ltable.h"
23 | #include "ltm.h"
24 | #include "lvm.h"
25 |
26 |
27 | static const char udatatypename[] = "userdata";
28 |
29 | LUAI_DDEF const char *const luaT_typenames_[LUA_TOTALTAGS] = {
30 | "no value",
31 | "nil", "boolean", udatatypename, "number",
32 | "string", "table", "function", udatatypename, "thread",
33 | "proto" /* this last case is used for tests only */
34 | };
35 |
36 |
37 | void luaT_init (lua_State *L) {
38 | static const char *const luaT_eventname[] = { /* ORDER TM */
39 | "__index", "__newindex",
40 | "__gc", "__mode", "__len", "__eq",
41 | "__add", "__sub", "__mul", "__mod", "__pow",
42 | "__div", "__idiv",
43 | "__band", "__bor", "__bxor", "__shl", "__shr",
44 | "__unm", "__bnot", "__lt", "__le",
45 | "__concat", "__call"
46 | };
47 | int i;
48 | for (i=0; itmname[i] = luaS_new(L, luaT_eventname[i]);
50 | luaC_fix(L, obj2gco(G(L)->tmname[i])); /* never collect these names */
51 | }
52 | }
53 |
54 |
55 | /*
56 | ** function to be used with macro "fasttm": optimized for absence of
57 | ** tag methods
58 | */
59 | const TValue *luaT_gettm (Table *events, TMS event, TString *ename) {
60 | const TValue *tm = luaH_getshortstr(events, ename);
61 | lua_assert(event <= TM_EQ);
62 | if (ttisnil(tm)) { /* no tag method? */
63 | events->flags |= cast_byte(1u<metatable;
75 | break;
76 | case LUA_TUSERDATA:
77 | mt = uvalue(o)->metatable;
78 | break;
79 | default:
80 | mt = G(L)->mt[ttnov(o)];
81 | }
82 | return (mt ? luaH_getshortstr(mt, G(L)->tmname[event]) : luaO_nilobject);
83 | }
84 |
85 |
86 | /*
87 | ** Return the name of the type of an object. For tables and userdata
88 | ** with metatable, use their '__name' metafield, if present.
89 | */
90 | const char *luaT_objtypename (lua_State *L, const TValue *o) {
91 | Table *mt;
92 | if ((ttistable(o) && (mt = hvalue(o)->metatable) != NULL) ||
93 | (ttisfulluserdata(o) && (mt = uvalue(o)->metatable) != NULL)) {
94 | const TValue *name = luaH_getshortstr(mt, luaS_new(L, "__name"));
95 | if (ttisstring(name)) /* is '__name' a string? */
96 | return getstr(tsvalue(name)); /* use it as type name */
97 | }
98 | return ttypename(ttnov(o)); /* else use standard type name */
99 | }
100 |
101 |
102 | void luaT_callTM (lua_State *L, const TValue *f, const TValue *p1,
103 | const TValue *p2, TValue *p3, int hasres) {
104 | ptrdiff_t result = savestack(L, p3);
105 | StkId func = L->top;
106 | setobj2s(L, func, f); /* push function (assume EXTRA_STACK) */
107 | setobj2s(L, func + 1, p1); /* 1st argument */
108 | setobj2s(L, func + 2, p2); /* 2nd argument */
109 | L->top += 3;
110 | if (!hasres) /* no result? 'p3' is third argument */
111 | setobj2s(L, L->top++, p3); /* 3rd argument */
112 | /* metamethod may yield only when called from Lua code */
113 | if (isLua(L->ci))
114 | luaD_call(L, func, hasres);
115 | else
116 | luaD_callnoyield(L, func, hasres);
117 | if (hasres) { /* if has result, move it to its place */
118 | p3 = restorestack(L, result);
119 | setobjs2s(L, p3, --L->top);
120 | }
121 | }
122 |
123 |
124 | int luaT_callbinTM (lua_State *L, const TValue *p1, const TValue *p2,
125 | StkId res, TMS event) {
126 | const TValue *tm = luaT_gettmbyobj(L, p1, event); /* try first operand */
127 | if (ttisnil(tm))
128 | tm = luaT_gettmbyobj(L, p2, event); /* try second operand */
129 | if (ttisnil(tm)) return 0;
130 | luaT_callTM(L, tm, p1, p2, res, 1);
131 | return 1;
132 | }
133 |
134 |
135 | void luaT_trybinTM (lua_State *L, const TValue *p1, const TValue *p2,
136 | StkId res, TMS event) {
137 | if (!luaT_callbinTM(L, p1, p2, res, event)) {
138 | switch (event) {
139 | case TM_CONCAT:
140 | luaG_concaterror(L, p1, p2);
141 | /* call never returns, but to avoid warnings: *//* FALLTHROUGH */
142 | case TM_BAND: case TM_BOR: case TM_BXOR:
143 | case TM_SHL: case TM_SHR: case TM_BNOT: {
144 | lua_Number dummy;
145 | if (tonumber(p1, &dummy) && tonumber(p2, &dummy))
146 | luaG_tointerror(L, p1, p2);
147 | else
148 | luaG_opinterror(L, p1, p2, "perform bitwise operation on");
149 | }
150 | /* calls never return, but to avoid warnings: *//* FALLTHROUGH */
151 | default:
152 | luaG_opinterror(L, p1, p2, "perform arithmetic on");
153 | }
154 | }
155 | }
156 |
157 |
158 | int luaT_callorderTM (lua_State *L, const TValue *p1, const TValue *p2,
159 | TMS event) {
160 | if (!luaT_callbinTM(L, p1, p2, L->top, event))
161 | return -1; /* no metamethod */
162 | else
163 | return !l_isfalse(L->top);
164 | }
165 |
166 |
--------------------------------------------------------------------------------
/lualib/src/main/cpp/lua/ltm.h:
--------------------------------------------------------------------------------
1 | /*
2 | ** $Id: ltm.h,v 2.22 2016/02/26 19:20:15 roberto Exp $
3 | ** Tag methods
4 | ** See Copyright Notice in lua.h
5 | */
6 |
7 | #ifndef ltm_h
8 | #define ltm_h
9 |
10 |
11 | #include "lobject.h"
12 |
13 |
14 | /*
15 | * WARNING: if you change the order of this enumeration,
16 | * grep "ORDER TM" and "ORDER OP"
17 | */
18 | typedef enum {
19 | TM_INDEX,
20 | TM_NEWINDEX,
21 | TM_GC,
22 | TM_MODE,
23 | TM_LEN,
24 | TM_EQ, /* last tag method with fast access */
25 | TM_ADD,
26 | TM_SUB,
27 | TM_MUL,
28 | TM_MOD,
29 | TM_POW,
30 | TM_DIV,
31 | TM_IDIV,
32 | TM_BAND,
33 | TM_BOR,
34 | TM_BXOR,
35 | TM_SHL,
36 | TM_SHR,
37 | TM_UNM,
38 | TM_BNOT,
39 | TM_LT,
40 | TM_LE,
41 | TM_CONCAT,
42 | TM_CALL,
43 | TM_N /* number of elements in the enum */
44 | } TMS;
45 |
46 |
47 |
48 | #define gfasttm(g,et,e) ((et) == NULL ? NULL : \
49 | ((et)->flags & (1u<<(e))) ? NULL : luaT_gettm(et, e, (g)->tmname[e]))
50 |
51 | #define fasttm(l,et,e) gfasttm(G(l), et, e)
52 |
53 | #define ttypename(x) luaT_typenames_[(x) + 1]
54 |
55 | LUAI_DDEC const char *const luaT_typenames_[LUA_TOTALTAGS];
56 |
57 |
58 | LUAI_FUNC const char *luaT_objtypename (lua_State *L, const TValue *o);
59 |
60 | LUAI_FUNC const TValue *luaT_gettm (Table *events, TMS event, TString *ename);
61 | LUAI_FUNC const TValue *luaT_gettmbyobj (lua_State *L, const TValue *o,
62 | TMS event);
63 | LUAI_FUNC void luaT_init (lua_State *L);
64 |
65 | LUAI_FUNC void luaT_callTM (lua_State *L, const TValue *f, const TValue *p1,
66 | const TValue *p2, TValue *p3, int hasres);
67 | LUAI_FUNC int luaT_callbinTM (lua_State *L, const TValue *p1, const TValue *p2,
68 | StkId res, TMS event);
69 | LUAI_FUNC void luaT_trybinTM (lua_State *L, const TValue *p1, const TValue *p2,
70 | StkId res, TMS event);
71 | LUAI_FUNC int luaT_callorderTM (lua_State *L, const TValue *p1,
72 | const TValue *p2, TMS event);
73 |
74 |
75 |
76 | #endif
77 |
--------------------------------------------------------------------------------
/lualib/src/main/cpp/lua/lua.hpp:
--------------------------------------------------------------------------------
1 | // lua.hpp
2 | // Lua header files for C++
3 | // <> not supplied automatically because Lua also compiles as C++
4 |
5 | extern "C" {
6 | #include "lua.h"
7 | #include "lualib.h"
8 | #include "lauxlib.h"
9 | }
10 |
--------------------------------------------------------------------------------
/lualib/src/main/cpp/lua/lualib.h:
--------------------------------------------------------------------------------
1 | /*
2 | ** $Id: lualib.h,v 1.45 2017/01/12 17:14:26 roberto Exp $
3 | ** Lua standard libraries
4 | ** See Copyright Notice in lua.h
5 | */
6 |
7 |
8 | #ifndef lualib_h
9 | #define lualib_h
10 |
11 | #include "lua.h"
12 |
13 |
14 | /* version suffix for environment variable names */
15 | #define LUA_VERSUFFIX "_" LUA_VERSION_MAJOR "_" LUA_VERSION_MINOR
16 |
17 |
18 | LUAMOD_API int (luaopen_base) (lua_State *L);
19 |
20 | #define LUA_COLIBNAME "coroutine"
21 | LUAMOD_API int (luaopen_coroutine) (lua_State *L);
22 |
23 | #define LUA_TABLIBNAME "table"
24 | LUAMOD_API int (luaopen_table) (lua_State *L);
25 |
26 | #define LUA_IOLIBNAME "io"
27 | LUAMOD_API int (luaopen_io) (lua_State *L);
28 |
29 | #define LUA_OSLIBNAME "os"
30 | LUAMOD_API int (luaopen_os) (lua_State *L);
31 |
32 | #define LUA_STRLIBNAME "string"
33 | LUAMOD_API int (luaopen_string) (lua_State *L);
34 |
35 | #define LUA_UTF8LIBNAME "utf8"
36 | LUAMOD_API int (luaopen_utf8) (lua_State *L);
37 |
38 | #define LUA_BITLIBNAME "bit32"
39 | LUAMOD_API int (luaopen_bit32) (lua_State *L);
40 |
41 | #define LUA_MATHLIBNAME "math"
42 | LUAMOD_API int (luaopen_math) (lua_State *L);
43 |
44 | #define LUA_DBLIBNAME "debug"
45 | LUAMOD_API int (luaopen_debug) (lua_State *L);
46 |
47 | #define LUA_LOADLIBNAME "package"
48 | LUAMOD_API int (luaopen_package) (lua_State *L);
49 |
50 |
51 | /* open all previous libraries */
52 | LUALIB_API void (luaL_openlibs) (lua_State *L);
53 |
54 |
55 |
56 | #if !defined(lua_assert)
57 | #define lua_assert(x) ((void)0)
58 | #endif
59 |
60 |
61 | #endif
62 |
--------------------------------------------------------------------------------
/lualib/src/main/cpp/lua/lundump.c:
--------------------------------------------------------------------------------
1 | /*
2 | ** $Id: lundump.c,v 2.44 2015/11/02 16:09:30 roberto Exp $
3 | ** load precompiled Lua chunks
4 | ** See Copyright Notice in lua.h
5 | */
6 |
7 | #define lundump_c
8 | #define LUA_CORE
9 |
10 | #include "lprefix.h"
11 |
12 |
13 | #include
14 |
15 | #include "lua.h"
16 |
17 | #include "ldebug.h"
18 | #include "ldo.h"
19 | #include "lfunc.h"
20 | #include "lmem.h"
21 | #include "lobject.h"
22 | #include "lstring.h"
23 | #include "lundump.h"
24 | #include "lzio.h"
25 |
26 |
27 | #if !defined(luai_verifycode)
28 | #define luai_verifycode(L,b,f) /* empty */
29 | #endif
30 |
31 |
32 | typedef struct {
33 | lua_State *L;
34 | ZIO *Z;
35 | const char *name;
36 | } LoadState;
37 |
38 |
39 | static l_noret error(LoadState *S, const char *why) {
40 | luaO_pushfstring(S->L, "%s: %s precompiled chunk", S->name, why);
41 | luaD_throw(S->L, LUA_ERRSYNTAX);
42 | }
43 |
44 |
45 | /*
46 | ** All high-level loads go through LoadVector; you can change it to
47 | ** adapt to the endianness of the input
48 | */
49 | #define LoadVector(S,b,n) LoadBlock(S,b,(n)*sizeof((b)[0]))
50 |
51 | static void LoadBlock (LoadState *S, void *b, size_t size) {
52 | if (luaZ_read(S->Z, b, size) != 0)
53 | error(S, "truncated");
54 | }
55 |
56 |
57 | #define LoadVar(S,x) LoadVector(S,&x,1)
58 |
59 |
60 | static lu_byte LoadByte (LoadState *S) {
61 | lu_byte x;
62 | LoadVar(S, x);
63 | return x;
64 | }
65 |
66 |
67 | static int LoadInt (LoadState *S) {
68 | int x;
69 | LoadVar(S, x);
70 | return x;
71 | }
72 |
73 |
74 | static lua_Number LoadNumber (LoadState *S) {
75 | lua_Number x;
76 | LoadVar(S, x);
77 | return x;
78 | }
79 |
80 |
81 | static lua_Integer LoadInteger (LoadState *S) {
82 | lua_Integer x;
83 | LoadVar(S, x);
84 | return x;
85 | }
86 |
87 |
88 | static TString *LoadString (LoadState *S) {
89 | size_t size = LoadByte(S);
90 | if (size == 0xFF)
91 | LoadVar(S, size);
92 | if (size == 0)
93 | return NULL;
94 | else if (--size <= LUAI_MAXSHORTLEN) { /* short string? */
95 | char buff[LUAI_MAXSHORTLEN];
96 | LoadVector(S, buff, size);
97 | return luaS_newlstr(S->L, buff, size);
98 | }
99 | else { /* long string */
100 | TString *ts = luaS_createlngstrobj(S->L, size);
101 | LoadVector(S, getstr(ts), size); /* load directly in final place */
102 | return ts;
103 | }
104 | }
105 |
106 |
107 | static void LoadCode (LoadState *S, Proto *f) {
108 | int n = LoadInt(S);
109 | f->code = luaM_newvector(S->L, n, Instruction);
110 | f->sizecode = n;
111 | LoadVector(S, f->code, n);
112 | }
113 |
114 |
115 | static void LoadFunction(LoadState *S, Proto *f, TString *psource);
116 |
117 |
118 | static void LoadConstants (LoadState *S, Proto *f) {
119 | int i;
120 | int n = LoadInt(S);
121 | f->k = luaM_newvector(S->L, n, TValue);
122 | f->sizek = n;
123 | for (i = 0; i < n; i++)
124 | setnilvalue(&f->k[i]);
125 | for (i = 0; i < n; i++) {
126 | TValue *o = &f->k[i];
127 | int t = LoadByte(S);
128 | switch (t) {
129 | case LUA_TNIL:
130 | setnilvalue(o);
131 | break;
132 | case LUA_TBOOLEAN:
133 | setbvalue(o, LoadByte(S));
134 | break;
135 | case LUA_TNUMFLT:
136 | setfltvalue(o, LoadNumber(S));
137 | break;
138 | case LUA_TNUMINT:
139 | setivalue(o, LoadInteger(S));
140 | break;
141 | case LUA_TSHRSTR:
142 | case LUA_TLNGSTR:
143 | setsvalue2n(S->L, o, LoadString(S));
144 | break;
145 | default:
146 | lua_assert(0);
147 | }
148 | }
149 | }
150 |
151 |
152 | static void LoadProtos (LoadState *S, Proto *f) {
153 | int i;
154 | int n = LoadInt(S);
155 | f->p = luaM_newvector(S->L, n, Proto *);
156 | f->sizep = n;
157 | for (i = 0; i < n; i++)
158 | f->p[i] = NULL;
159 | for (i = 0; i < n; i++) {
160 | f->p[i] = luaF_newproto(S->L);
161 | LoadFunction(S, f->p[i], f->source);
162 | }
163 | }
164 |
165 |
166 | static void LoadUpvalues (LoadState *S, Proto *f) {
167 | int i, n;
168 | n = LoadInt(S);
169 | f->upvalues = luaM_newvector(S->L, n, Upvaldesc);
170 | f->sizeupvalues = n;
171 | for (i = 0; i < n; i++)
172 | f->upvalues[i].name = NULL;
173 | for (i = 0; i < n; i++) {
174 | f->upvalues[i].instack = LoadByte(S);
175 | f->upvalues[i].idx = LoadByte(S);
176 | }
177 | }
178 |
179 |
180 | static void LoadDebug (LoadState *S, Proto *f) {
181 | int i, n;
182 | n = LoadInt(S);
183 | f->lineinfo = luaM_newvector(S->L, n, int);
184 | f->sizelineinfo = n;
185 | LoadVector(S, f->lineinfo, n);
186 | n = LoadInt(S);
187 | f->locvars = luaM_newvector(S->L, n, LocVar);
188 | f->sizelocvars = n;
189 | for (i = 0; i < n; i++)
190 | f->locvars[i].varname = NULL;
191 | for (i = 0; i < n; i++) {
192 | f->locvars[i].varname = LoadString(S);
193 | f->locvars[i].startpc = LoadInt(S);
194 | f->locvars[i].endpc = LoadInt(S);
195 | }
196 | n = LoadInt(S);
197 | for (i = 0; i < n; i++)
198 | f->upvalues[i].name = LoadString(S);
199 | }
200 |
201 |
202 | static void LoadFunction (LoadState *S, Proto *f, TString *psource) {
203 | f->source = LoadString(S);
204 | if (f->source == NULL) /* no source in dump? */
205 | f->source = psource; /* reuse parent's source */
206 | f->linedefined = LoadInt(S);
207 | f->lastlinedefined = LoadInt(S);
208 | f->numparams = LoadByte(S);
209 | f->is_vararg = LoadByte(S);
210 | f->maxstacksize = LoadByte(S);
211 | LoadCode(S, f);
212 | LoadConstants(S, f);
213 | LoadUpvalues(S, f);
214 | LoadProtos(S, f);
215 | LoadDebug(S, f);
216 | }
217 |
218 |
219 | static void checkliteral (LoadState *S, const char *s, const char *msg) {
220 | char buff[sizeof(LUA_SIGNATURE) + sizeof(LUAC_DATA)]; /* larger than both */
221 | size_t len = strlen(s);
222 | LoadVector(S, buff, len);
223 | if (memcmp(s, buff, len) != 0)
224 | error(S, msg);
225 | }
226 |
227 |
228 | static void fchecksize (LoadState *S, size_t size, const char *tname) {
229 | if (LoadByte(S) != size)
230 | error(S, luaO_pushfstring(S->L, "%s size mismatch in", tname));
231 | }
232 |
233 |
234 | #define checksize(S,t) fchecksize(S,sizeof(t),#t)
235 |
236 | static void checkHeader (LoadState *S) {
237 | checkliteral(S, LUA_SIGNATURE + 1, "not a"); /* 1st char already checked */
238 | if (LoadByte(S) != LUAC_VERSION)
239 | error(S, "version mismatch in");
240 | if (LoadByte(S) != LUAC_FORMAT)
241 | error(S, "format mismatch in");
242 | checkliteral(S, LUAC_DATA, "corrupted");
243 | checksize(S, int);
244 | checksize(S, size_t);
245 | checksize(S, Instruction);
246 | checksize(S, lua_Integer);
247 | checksize(S, lua_Number);
248 | if (LoadInteger(S) != LUAC_INT)
249 | error(S, "endianness mismatch in");
250 | if (LoadNumber(S) != LUAC_NUM)
251 | error(S, "float format mismatch in");
252 | }
253 |
254 |
255 | /*
256 | ** load precompiled chunk
257 | */
258 | LClosure *luaU_undump(lua_State *L, ZIO *Z, const char *name) {
259 | LoadState S;
260 | LClosure *cl;
261 | if (*name == '@' || *name == '=')
262 | S.name = name + 1;
263 | else if (*name == LUA_SIGNATURE[0])
264 | S.name = "binary string";
265 | else
266 | S.name = name;
267 | S.L = L;
268 | S.Z = Z;
269 | checkHeader(&S);
270 | cl = luaF_newLclosure(L, LoadByte(&S));
271 | setclLvalue(L, L->top, cl);
272 | luaD_inctop(L);
273 | cl->p = luaF_newproto(L);
274 | LoadFunction(&S, cl->p, NULL);
275 | lua_assert(cl->nupvalues == cl->p->sizeupvalues);
276 | luai_verifycode(L, buff, cl->p);
277 | return cl;
278 | }
279 |
280 |
--------------------------------------------------------------------------------
/lualib/src/main/cpp/lua/lundump.h:
--------------------------------------------------------------------------------
1 | /*
2 | ** $Id: lundump.h,v 1.45 2015/09/08 15:41:05 roberto Exp $
3 | ** load precompiled Lua chunks
4 | ** See Copyright Notice in lua.h
5 | */
6 |
7 | #ifndef lundump_h
8 | #define lundump_h
9 |
10 | #include "llimits.h"
11 | #include "lobject.h"
12 | #include "lzio.h"
13 |
14 |
15 | /* data to catch conversion errors */
16 | #define LUAC_DATA "\x19\x93\r\n\x1a\n"
17 |
18 | #define LUAC_INT 0x5678
19 | #define LUAC_NUM cast_num(370.5)
20 |
21 | #define MYINT(s) (s[0]-'0')
22 | #define LUAC_VERSION (MYINT(LUA_VERSION_MAJOR)*16+MYINT(LUA_VERSION_MINOR))
23 | #define LUAC_FORMAT 0 /* this is the official format */
24 |
25 | /* load one chunk; from lundump.c */
26 | LUAI_FUNC LClosure* luaU_undump (lua_State* L, ZIO* Z, const char* name);
27 |
28 | /* dump one chunk; from ldump.c */
29 | LUAI_FUNC int luaU_dump (lua_State* L, const Proto* f, lua_Writer w,
30 | void* data, int strip);
31 |
32 | #endif
33 |
--------------------------------------------------------------------------------
/lualib/src/main/cpp/lua/lutf8lib.c:
--------------------------------------------------------------------------------
1 | /*
2 | ** $Id: lutf8lib.c,v 1.16 2016/12/22 13:08:50 roberto Exp $
3 | ** Standard library for UTF-8 manipulation
4 | ** See Copyright Notice in lua.h
5 | */
6 |
7 | #define lutf8lib_c
8 | #define LUA_LIB
9 |
10 | #include "lprefix.h"
11 |
12 |
13 | #include
14 | #include
15 | #include
16 | #include
17 |
18 | #include "lua.h"
19 |
20 | #include "lauxlib.h"
21 | #include "lualib.h"
22 |
23 | #define MAXUNICODE 0x10FFFF
24 |
25 | #define iscont(p) ((*(p) & 0xC0) == 0x80)
26 |
27 |
28 | /* from strlib */
29 | /* translate a relative string position: negative means back from end */
30 | static lua_Integer u_posrelat (lua_Integer pos, size_t len) {
31 | if (pos >= 0) return pos;
32 | else if (0u - (size_t)pos > len) return 0;
33 | else return (lua_Integer)len + pos + 1;
34 | }
35 |
36 |
37 | /*
38 | ** Decode one UTF-8 sequence, returning NULL if byte sequence is invalid.
39 | */
40 | static const char *utf8_decode (const char *o, int *val) {
41 | static const unsigned int limits[] = {0xFF, 0x7F, 0x7FF, 0xFFFF};
42 | const unsigned char *s = (const unsigned char *)o;
43 | unsigned int c = s[0];
44 | unsigned int res = 0; /* final result */
45 | if (c < 0x80) /* ascii? */
46 | res = c;
47 | else {
48 | int count = 0; /* to count number of continuation bytes */
49 | while (c & 0x40) { /* still have continuation bytes? */
50 | int cc = s[++count]; /* read next byte */
51 | if ((cc & 0xC0) != 0x80) /* not a continuation byte? */
52 | return NULL; /* invalid byte sequence */
53 | res = (res << 6) | (cc & 0x3F); /* add lower 6 bits from cont. byte */
54 | c <<= 1; /* to test next bit */
55 | }
56 | res |= ((c & 0x7F) << (count * 5)); /* add first byte */
57 | if (count > 3 || res > MAXUNICODE || res <= limits[count])
58 | return NULL; /* invalid byte sequence */
59 | s += count; /* skip continuation bytes read */
60 | }
61 | if (val) *val = res;
62 | return (const char *)s + 1; /* +1 to include first byte */
63 | }
64 |
65 |
66 | /*
67 | ** utf8len(s [, i [, j]]) --> number of characters that start in the
68 | ** range [i,j], or nil + current position if 's' is not well formed in
69 | ** that interval
70 | */
71 | static int utflen (lua_State *L) {
72 | int n = 0;
73 | size_t len;
74 | const char *s = luaL_checklstring(L, 1, &len);
75 | lua_Integer posi = u_posrelat(luaL_optinteger(L, 2, 1), len);
76 | lua_Integer posj = u_posrelat(luaL_optinteger(L, 3, -1), len);
77 | luaL_argcheck(L, 1 <= posi && --posi <= (lua_Integer)len, 2,
78 | "initial position out of string");
79 | luaL_argcheck(L, --posj < (lua_Integer)len, 3,
80 | "final position out of string");
81 | while (posi <= posj) {
82 | const char *s1 = utf8_decode(s + posi, NULL);
83 | if (s1 == NULL) { /* conversion error? */
84 | lua_pushnil(L); /* return nil ... */
85 | lua_pushinteger(L, posi + 1); /* ... and current position */
86 | return 2;
87 | }
88 | posi = s1 - s;
89 | n++;
90 | }
91 | lua_pushinteger(L, n);
92 | return 1;
93 | }
94 |
95 |
96 | /*
97 | ** codepoint(s, [i, [j]]) -> returns codepoints for all characters
98 | ** that start in the range [i,j]
99 | */
100 | static int codepoint (lua_State *L) {
101 | size_t len;
102 | const char *s = luaL_checklstring(L, 1, &len);
103 | lua_Integer posi = u_posrelat(luaL_optinteger(L, 2, 1), len);
104 | lua_Integer pose = u_posrelat(luaL_optinteger(L, 3, posi), len);
105 | int n;
106 | const char *se;
107 | luaL_argcheck(L, posi >= 1, 2, "out of range");
108 | luaL_argcheck(L, pose <= (lua_Integer)len, 3, "out of range");
109 | if (posi > pose) return 0; /* empty interval; return no values */
110 | if (pose - posi >= INT_MAX) /* (lua_Integer -> int) overflow? */
111 | return luaL_error(L, "string slice too long");
112 | n = (int)(pose - posi) + 1;
113 | luaL_checkstack(L, n, "string slice too long");
114 | n = 0;
115 | se = s + pose;
116 | for (s += posi - 1; s < se;) {
117 | int code;
118 | s = utf8_decode(s, &code);
119 | if (s == NULL)
120 | return luaL_error(L, "invalid UTF-8 code");
121 | lua_pushinteger(L, code);
122 | n++;
123 | }
124 | return n;
125 | }
126 |
127 |
128 | static void pushutfchar (lua_State *L, int arg) {
129 | lua_Integer code = luaL_checkinteger(L, arg);
130 | luaL_argcheck(L, 0 <= code && code <= MAXUNICODE, arg, "value out of range");
131 | lua_pushfstring(L, "%U", (long)code);
132 | }
133 |
134 |
135 | /*
136 | ** utfchar(n1, n2, ...) -> char(n1)..char(n2)...
137 | */
138 | static int utfchar (lua_State *L) {
139 | int n = lua_gettop(L); /* number of arguments */
140 | if (n == 1) /* optimize common case of single char */
141 | pushutfchar(L, 1);
142 | else {
143 | int i;
144 | luaL_Buffer b;
145 | luaL_buffinit(L, &b);
146 | for (i = 1; i <= n; i++) {
147 | pushutfchar(L, i);
148 | luaL_addvalue(&b);
149 | }
150 | luaL_pushresult(&b);
151 | }
152 | return 1;
153 | }
154 |
155 |
156 | /*
157 | ** offset(s, n, [i]) -> index where n-th character counting from
158 | ** position 'i' starts; 0 means character at 'i'.
159 | */
160 | static int byteoffset (lua_State *L) {
161 | size_t len;
162 | const char *s = luaL_checklstring(L, 1, &len);
163 | lua_Integer n = luaL_checkinteger(L, 2);
164 | lua_Integer posi = (n >= 0) ? 1 : len + 1;
165 | posi = u_posrelat(luaL_optinteger(L, 3, posi), len);
166 | luaL_argcheck(L, 1 <= posi && --posi <= (lua_Integer)len, 3,
167 | "position out of range");
168 | if (n == 0) {
169 | /* find beginning of current byte sequence */
170 | while (posi > 0 && iscont(s + posi)) posi--;
171 | }
172 | else {
173 | if (iscont(s + posi))
174 | luaL_error(L, "initial position is a continuation byte");
175 | if (n < 0) {
176 | while (n < 0 && posi > 0) { /* move back */
177 | do { /* find beginning of previous character */
178 | posi--;
179 | } while (posi > 0 && iscont(s + posi));
180 | n++;
181 | }
182 | }
183 | else {
184 | n--; /* do not move for 1st character */
185 | while (n > 0 && posi < (lua_Integer)len) {
186 | do { /* find beginning of next character */
187 | posi++;
188 | } while (iscont(s + posi)); /* (cannot pass final '\0') */
189 | n--;
190 | }
191 | }
192 | }
193 | if (n == 0) /* did it find given character? */
194 | lua_pushinteger(L, posi + 1);
195 | else /* no such character */
196 | lua_pushnil(L);
197 | return 1;
198 | }
199 |
200 |
201 | static int iter_aux (lua_State *L) {
202 | size_t len;
203 | const char *s = luaL_checklstring(L, 1, &len);
204 | lua_Integer n = lua_tointeger(L, 2) - 1;
205 | if (n < 0) /* first iteration? */
206 | n = 0; /* start from here */
207 | else if (n < (lua_Integer)len) {
208 | n++; /* skip current byte */
209 | while (iscont(s + n)) n++; /* and its continuations */
210 | }
211 | if (n >= (lua_Integer)len)
212 | return 0; /* no more codepoints */
213 | else {
214 | int code;
215 | const char *next = utf8_decode(s + n, &code);
216 | if (next == NULL || iscont(next))
217 | return luaL_error(L, "invalid UTF-8 code");
218 | lua_pushinteger(L, n + 1);
219 | lua_pushinteger(L, code);
220 | return 2;
221 | }
222 | }
223 |
224 |
225 | static int iter_codes (lua_State *L) {
226 | luaL_checkstring(L, 1);
227 | lua_pushcfunction(L, iter_aux);
228 | lua_pushvalue(L, 1);
229 | lua_pushinteger(L, 0);
230 | return 3;
231 | }
232 |
233 |
234 | /* pattern to match a single UTF-8 character */
235 | #define UTF8PATT "[\0-\x7F\xC2-\xF4][\x80-\xBF]*"
236 |
237 |
238 | static const luaL_Reg funcs[] = {
239 | {"offset", byteoffset},
240 | {"codepoint", codepoint},
241 | {"char", utfchar},
242 | {"len", utflen},
243 | {"codes", iter_codes},
244 | /* placeholders */
245 | {"charpattern", NULL},
246 | {NULL, NULL}
247 | };
248 |
249 |
250 | LUAMOD_API int luaopen_utf8 (lua_State *L) {
251 | luaL_newlib(L, funcs);
252 | lua_pushlstring(L, UTF8PATT, sizeof(UTF8PATT)/sizeof(char) - 1);
253 | lua_setfield(L, -2, "charpattern");
254 | return 1;
255 | }
256 |
257 |
--------------------------------------------------------------------------------
/lualib/src/main/cpp/lua/lvm.h:
--------------------------------------------------------------------------------
1 | /*
2 | ** $Id: lvm.h,v 2.41 2016/12/22 13:08:50 roberto Exp $
3 | ** Lua virtual machine
4 | ** See Copyright Notice in lua.h
5 | */
6 |
7 | #ifndef lvm_h
8 | #define lvm_h
9 |
10 |
11 | #include "ldo.h"
12 | #include "lobject.h"
13 | #include "ltm.h"
14 |
15 |
16 | #if !defined(LUA_NOCVTN2S)
17 | #define cvt2str(o) ttisnumber(o)
18 | #else
19 | #define cvt2str(o) 0 /* no conversion from numbers to strings */
20 | #endif
21 |
22 |
23 | #if !defined(LUA_NOCVTS2N)
24 | #define cvt2num(o) ttisstring(o)
25 | #else
26 | #define cvt2num(o) 0 /* no conversion from strings to numbers */
27 | #endif
28 |
29 |
30 | /*
31 | ** You can define LUA_FLOORN2I if you want to convert floats to integers
32 | ** by flooring them (instead of raising an error if they are not
33 | ** integral values)
34 | */
35 | #if !defined(LUA_FLOORN2I)
36 | #define LUA_FLOORN2I 0
37 | #endif
38 |
39 |
40 | #define tonumber(o,n) \
41 | (ttisfloat(o) ? (*(n) = fltvalue(o), 1) : luaV_tonumber_(o,n))
42 |
43 | #define tointeger(o,i) \
44 | (ttisinteger(o) ? (*(i) = ivalue(o), 1) : luaV_tointeger(o,i,LUA_FLOORN2I))
45 |
46 | #define intop(op,v1,v2) l_castU2S(l_castS2U(v1) op l_castS2U(v2))
47 |
48 | #define luaV_rawequalobj(t1,t2) luaV_equalobj(NULL,t1,t2)
49 |
50 |
51 | /*
52 | ** fast track for 'gettable': if 't' is a table and 't[k]' is not nil,
53 | ** return 1 with 'slot' pointing to 't[k]' (final result). Otherwise,
54 | ** return 0 (meaning it will have to check metamethod) with 'slot'
55 | ** pointing to a nil 't[k]' (if 't' is a table) or NULL (otherwise).
56 | ** 'f' is the raw get function to use.
57 | */
58 | #define luaV_fastget(L,t,k,slot,f) \
59 | (!ttistable(t) \
60 | ? (slot = NULL, 0) /* not a table; 'slot' is NULL and result is 0 */ \
61 | : (slot = f(hvalue(t), k), /* else, do raw access */ \
62 | !ttisnil(slot))) /* result not nil? */
63 |
64 | /*
65 | ** standard implementation for 'gettable'
66 | */
67 | #define luaV_gettable(L,t,k,v) { const TValue *slot; \
68 | if (luaV_fastget(L,t,k,slot,luaH_get)) { setobj2s(L, v, slot); } \
69 | else luaV_finishget(L,t,k,v,slot); }
70 |
71 |
72 | /*
73 | ** Fast track for set table. If 't' is a table and 't[k]' is not nil,
74 | ** call GC barrier, do a raw 't[k]=v', and return true; otherwise,
75 | ** return false with 'slot' equal to NULL (if 't' is not a table) or
76 | ** 'nil'. (This is needed by 'luaV_finishget'.) Note that, if the macro
77 | ** returns true, there is no need to 'invalidateTMcache', because the
78 | ** call is not creating a new entry.
79 | */
80 | #define luaV_fastset(L,t,k,slot,f,v) \
81 | (!ttistable(t) \
82 | ? (slot = NULL, 0) \
83 | : (slot = f(hvalue(t), k), \
84 | ttisnil(slot) ? 0 \
85 | : (luaC_barrierback(L, hvalue(t), v), \
86 | setobj2t(L, cast(TValue *,slot), v), \
87 | 1)))
88 |
89 |
90 | #define luaV_settable(L,t,k,v) { const TValue *slot; \
91 | if (!luaV_fastset(L,t,k,slot,luaH_get,v)) \
92 | luaV_finishset(L,t,k,v,slot); }
93 |
94 |
95 |
96 | LUAI_FUNC int luaV_equalobj (lua_State *L, const TValue *t1, const TValue *t2);
97 | LUAI_FUNC int luaV_lessthan (lua_State *L, const TValue *l, const TValue *r);
98 | LUAI_FUNC int luaV_lessequal (lua_State *L, const TValue *l, const TValue *r);
99 | LUAI_FUNC int luaV_tonumber_ (const TValue *obj, lua_Number *n);
100 | LUAI_FUNC int luaV_tointeger (const TValue *obj, lua_Integer *p, int mode);
101 | LUAI_FUNC void luaV_finishget (lua_State *L, const TValue *t, TValue *key,
102 | StkId val, const TValue *slot);
103 | LUAI_FUNC void luaV_finishset (lua_State *L, const TValue *t, TValue *key,
104 | StkId val, const TValue *slot);
105 | LUAI_FUNC void luaV_finishOp (lua_State *L);
106 | LUAI_FUNC void luaV_execute (lua_State *L);
107 | LUAI_FUNC void luaV_concat (lua_State *L, int total);
108 | LUAI_FUNC lua_Integer luaV_div (lua_State *L, lua_Integer x, lua_Integer y);
109 | LUAI_FUNC lua_Integer luaV_mod (lua_State *L, lua_Integer x, lua_Integer y);
110 | LUAI_FUNC lua_Integer luaV_shiftl (lua_Integer x, lua_Integer y);
111 | LUAI_FUNC void luaV_objlen (lua_State *L, StkId ra, const TValue *rb);
112 |
113 | #endif
114 |
--------------------------------------------------------------------------------
/lualib/src/main/cpp/lua/lzio.c:
--------------------------------------------------------------------------------
1 | /*
2 | ** $Id: lzio.c,v 1.37 2015/09/08 15:41:05 roberto Exp $
3 | ** Buffered streams
4 | ** See Copyright Notice in lua.h
5 | */
6 |
7 | #define lzio_c
8 | #define LUA_CORE
9 |
10 | #include "lprefix.h"
11 |
12 |
13 | #include
14 |
15 | #include "lua.h"
16 |
17 | #include "llimits.h"
18 | #include "lmem.h"
19 | #include "lstate.h"
20 | #include "lzio.h"
21 |
22 |
23 | int luaZ_fill (ZIO *z) {
24 | size_t size;
25 | lua_State *L = z->L;
26 | const char *buff;
27 | lua_unlock(L);
28 | buff = z->reader(L, z->data, &size);
29 | lua_lock(L);
30 | if (buff == NULL || size == 0)
31 | return EOZ;
32 | z->n = size - 1; /* discount char being returned */
33 | z->p = buff;
34 | return cast_uchar(*(z->p++));
35 | }
36 |
37 |
38 | void luaZ_init (lua_State *L, ZIO *z, lua_Reader reader, void *data) {
39 | z->L = L;
40 | z->reader = reader;
41 | z->data = data;
42 | z->n = 0;
43 | z->p = NULL;
44 | }
45 |
46 |
47 | /* --------------------------------------------------------------- read --- */
48 | size_t luaZ_read (ZIO *z, void *b, size_t n) {
49 | while (n) {
50 | size_t m;
51 | if (z->n == 0) { /* no bytes in buffer? */
52 | if (luaZ_fill(z) == EOZ) /* try to read more */
53 | return n; /* no more input; return number of missing bytes */
54 | else {
55 | z->n++; /* luaZ_fill consumed first byte; put it back */
56 | z->p--;
57 | }
58 | }
59 | m = (n <= z->n) ? n : z->n; /* min. between n and z->n */
60 | memcpy(b, z->p, m);
61 | z->n -= m;
62 | z->p += m;
63 | b = (char *)b + m;
64 | n -= m;
65 | }
66 | return 0;
67 | }
68 |
69 |
--------------------------------------------------------------------------------
/lualib/src/main/cpp/lua/lzio.h:
--------------------------------------------------------------------------------
1 | /*
2 | ** $Id: lzio.h,v 1.31 2015/09/08 15:41:05 roberto Exp $
3 | ** Buffered streams
4 | ** See Copyright Notice in lua.h
5 | */
6 |
7 |
8 | #ifndef lzio_h
9 | #define lzio_h
10 |
11 | #include "lua.h"
12 |
13 | #include "lmem.h"
14 |
15 |
16 | #define EOZ (-1) /* end of stream */
17 |
18 | typedef struct Zio ZIO;
19 |
20 | #define zgetc(z) (((z)->n--)>0 ? cast_uchar(*(z)->p++) : luaZ_fill(z))
21 |
22 |
23 | typedef struct Mbuffer {
24 | char *buffer;
25 | size_t n;
26 | size_t buffsize;
27 | } Mbuffer;
28 |
29 | #define luaZ_initbuffer(L, buff) ((buff)->buffer = NULL, (buff)->buffsize = 0)
30 |
31 | #define luaZ_buffer(buff) ((buff)->buffer)
32 | #define luaZ_sizebuffer(buff) ((buff)->buffsize)
33 | #define luaZ_bufflen(buff) ((buff)->n)
34 |
35 | #define luaZ_buffremove(buff,i) ((buff)->n -= (i))
36 | #define luaZ_resetbuffer(buff) ((buff)->n = 0)
37 |
38 |
39 | #define luaZ_resizebuffer(L, buff, size) \
40 | ((buff)->buffer = luaM_reallocvchar(L, (buff)->buffer, \
41 | (buff)->buffsize, size), \
42 | (buff)->buffsize = size)
43 |
44 | #define luaZ_freebuffer(L, buff) luaZ_resizebuffer(L, buff, 0)
45 |
46 |
47 | LUAI_FUNC void luaZ_init (lua_State *L, ZIO *z, lua_Reader reader,
48 | void *data);
49 | LUAI_FUNC size_t luaZ_read (ZIO* z, void *b, size_t n); /* read next n bytes */
50 |
51 |
52 |
53 | /* --------- Private Part ------------------ */
54 |
55 | struct Zio {
56 | size_t n; /* bytes still unread */
57 | const char *p; /* current position in buffer */
58 | lua_Reader reader; /* reader function */
59 | void *data; /* additional data */
60 | lua_State *L; /* Lua state (for reader) */
61 | };
62 |
63 |
64 | LUAI_FUNC int luaZ_fill (ZIO *z);
65 |
66 | #endif
67 |
--------------------------------------------------------------------------------
/lualib/src/main/java/com/jmengxy/lualib/Lua.java:
--------------------------------------------------------------------------------
1 | package com.jmengxy.lualib;
2 |
3 | import android.util.Pair;
4 |
5 | import static android.util.Pair.create;
6 |
7 | public final class Lua {
8 |
9 | public static final int LUA_TYPE_NONE = -1;
10 | public static final int LUA_TYPE_NIL = 0;
11 | public static final int LUA_TYPE_BOOLEAN = 1;
12 | public static final int LUA_TYPE_LIGHTUSERDATA = 2;
13 | public static final int LUA_TYPE_NUMBER = 3;
14 | public static final int LUA_TYPE_STRING = 4;
15 | public static final int LUA_TYPE_TABLE = 5;
16 | public static final int LUA_TYPE_FUNCTION = 6;
17 | public static final int LUA_TYPE_USERDATA = 7;
18 | public static final int LUA_TYPE_THREAD = 8;
19 | public static final int LUA_TYPE_NUMTAGS = 9;
20 |
21 | private static final String ERROR_LUA_LOCAL_OBJECT_IS_DESTROYED = "Lua local object is destroyed!";
22 |
23 | static {
24 | System.loadLibrary("luax");
25 | }
26 |
27 | private long luaState = 0;
28 |
29 | public Lua() {
30 | luaState = newLuaState();
31 | }
32 |
33 | private static native long newLuaState();
34 |
35 | private static native void deleteLuaState(long luaStatePtr);
36 |
37 | private static native int luaParseLine(long luaStatePtr, String line);
38 |
39 | private static native int luaParseFile(long luaStatePtr, String file);
40 |
41 | private static native String luaGetError(long luaStatePtr, int errCode);
42 |
43 | private static native void luaGetGlobal(long luaStatePtr, String name);
44 |
45 | private static native void luaSetGlobal(long luaStatePtr, String name);
46 |
47 | private static native int luaGetType(long luaStatePtr, int index);
48 |
49 | private static native void luaPop(long luaStatePtr, int index);
50 |
51 | private static native void luaPushString(long luaStatePtr, String value);
52 |
53 | private static native String luaToString(long luaStatePtr, int index, String defaultValue);
54 |
55 | private static native void luaPushBoolean(long luaStatePtr, boolean value);
56 |
57 | private static native boolean luaToBoolean(long luaStatePtr, int index, boolean defaultValue);
58 |
59 | private static native boolean luaIsInteger(long luaStatePtr, int index);
60 |
61 | private static native void luaPushInteger(long luaStatePtr, int value);
62 |
63 | private static native int luaToInteger(long luaStatePtr, int index, int defaultValue);
64 |
65 | private static native void luaPushDouble(long luaStatePtr, double value);
66 |
67 | private static native double luaToDouble(long luaStatePtr, int index, double defaultValue);
68 |
69 | public Pair parseLine(String line) {
70 | if (0 == luaState) {
71 | throw new RuntimeException(ERROR_LUA_LOCAL_OBJECT_IS_DESTROYED);
72 | }
73 |
74 | int errCode = luaParseLine(luaState, line);
75 | String message = luaGetError(luaState, errCode);
76 |
77 | return create(0 == errCode, message);
78 | }
79 |
80 | public Pair parseFile(String file) {
81 | if (0 == luaState) {
82 | throw new RuntimeException(ERROR_LUA_LOCAL_OBJECT_IS_DESTROYED);
83 | }
84 |
85 | int errCode = luaParseFile(luaState, file);
86 | String message = luaGetError(luaState, errCode);
87 |
88 | return create(0 == errCode, message);
89 | }
90 |
91 | //return LUA_TYPE_XXX
92 | public int getType(String name) {
93 | if (0 == luaState) {
94 | throw new RuntimeException(ERROR_LUA_LOCAL_OBJECT_IS_DESTROYED);
95 | }
96 |
97 | luaGetGlobal(luaState, name);
98 | int type = luaGetType(luaState, 1);
99 | luaPop(luaState, -1);
100 | return type;
101 | }
102 |
103 | public String getString(String name, String defaultValue) {
104 | if (0 == luaState) {
105 | throw new RuntimeException(ERROR_LUA_LOCAL_OBJECT_IS_DESTROYED);
106 | }
107 |
108 | luaGetGlobal(luaState, name);
109 | String s = luaToString(luaState, 1, defaultValue);
110 | luaPop(luaState, -1);
111 | return s;
112 | }
113 |
114 | public void setString(String name, String value) {
115 | if (0 == luaState) {
116 | throw new RuntimeException(ERROR_LUA_LOCAL_OBJECT_IS_DESTROYED);
117 | }
118 |
119 | luaPushString(luaState, value);
120 | luaSetGlobal(luaState, name);
121 | }
122 |
123 | public boolean getBoolean(String name, boolean defaultValue) {
124 | if (0 == luaState) {
125 | throw new RuntimeException(ERROR_LUA_LOCAL_OBJECT_IS_DESTROYED);
126 | }
127 |
128 | luaGetGlobal(luaState, name);
129 | boolean value = luaToBoolean(luaState, 1, defaultValue);
130 | luaPop(luaState, -1);
131 | return value;
132 | }
133 |
134 | public void setBoolean(String name, boolean value) {
135 | if (0 == luaState) {
136 | throw new RuntimeException(ERROR_LUA_LOCAL_OBJECT_IS_DESTROYED);
137 | }
138 |
139 | luaPushBoolean(luaState, value);
140 | luaSetGlobal(luaState, name);
141 | }
142 |
143 | public boolean isInteger(String name) {
144 | if (0 == luaState) {
145 | throw new RuntimeException(ERROR_LUA_LOCAL_OBJECT_IS_DESTROYED);
146 | }
147 |
148 | luaGetGlobal(luaState, name);
149 | boolean isInteger = luaIsInteger(luaState, 1);
150 | luaPop(luaState, -1);
151 | return isInteger;
152 | }
153 |
154 | public int getInteger(String name, int defaultValue) {
155 | if (0 == luaState) {
156 | throw new RuntimeException(ERROR_LUA_LOCAL_OBJECT_IS_DESTROYED);
157 | }
158 |
159 | luaGetGlobal(luaState, name);
160 | int value = luaToInteger(luaState, 1, defaultValue);
161 | luaPop(luaState, -1);
162 | return value;
163 | }
164 |
165 | public void setInteger(String name, int value) {
166 | if (0 == luaState) {
167 | throw new RuntimeException(ERROR_LUA_LOCAL_OBJECT_IS_DESTROYED);
168 | }
169 |
170 | luaPushInteger(luaState, value);
171 | luaSetGlobal(luaState, name);
172 | }
173 |
174 | public double getDouble(String name, double defaultValue) {
175 | if (0 == luaState) {
176 | throw new RuntimeException(ERROR_LUA_LOCAL_OBJECT_IS_DESTROYED);
177 | }
178 |
179 | luaGetGlobal(luaState, name);
180 | double value = luaToDouble(luaState, 1, defaultValue);
181 | luaPop(luaState, -1);
182 | return value;
183 | }
184 |
185 | public void setDouble(String name, double value) {
186 | if (0 == luaState) {
187 | throw new RuntimeException(ERROR_LUA_LOCAL_OBJECT_IS_DESTROYED);
188 | }
189 |
190 | luaPushDouble(luaState, value);
191 | luaSetGlobal(luaState, name);
192 | }
193 |
194 | public void close() {
195 | if (luaState != 0) {
196 | deleteLuaState(luaState);
197 | luaState = 0;
198 | }
199 | }
200 |
201 | @Override
202 | protected void finalize() throws Throwable {
203 | close();
204 | super.finalize();
205 | }
206 | }
207 |
--------------------------------------------------------------------------------
/lualib/src/main/res/values/values.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 | LuaLib
4 |
5 |
--------------------------------------------------------------------------------
/settings.gradle:
--------------------------------------------------------------------------------
1 | include ':app', ':lualib'
2 |
--------------------------------------------------------------------------------