Skip to content

Commit

Permalink
Update to latest ballerina version
Browse files Browse the repository at this point in the history
  • Loading branch information
lankavitharana committed Dec 11, 2017
1 parent 208e24a commit b5b3f68
Show file tree
Hide file tree
Showing 8 changed files with 60 additions and 193 deletions.
18 changes: 5 additions & 13 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,25 +10,17 @@ This is currently depends on ballerina 0.89 release. Since ballerina jars are no

- Copy the jar file to BALLERINA_HOME/bre/lib folder

Then you can use functions in "wso2.ballerina.math" package within your ballerina files.
Then you can use functions in "wso2.sample.math" package within your ballerina files.

Sample usage would be as follows

```
import ballerina.lang.system;
import wso2.ballerina.math;
import wso2.sample.math;
function main (string[] args) {
float a = 2;
float b = math:exp(a);
float c = 2.0;
float d = 2.0;
float e = math:pow(c,d);
float f = math:random();
system:println(b);
system:println(e);
system:println(f);
int a = 8;
int b = math:nextInt(a);
println(b);
}
```
Expand Down
32 changes: 8 additions & 24 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -29,13 +29,13 @@
<dependencies>
<dependency>
<groupId>org.ballerinalang</groupId>
<artifactId>annotation-processor</artifactId>
<version>0.89</version>
<artifactId>ballerina-lang</artifactId>
<version>0.95.1</version>
</dependency>
<dependency>
<groupId>org.ballerinalang</groupId>
<artifactId>ballerina-native</artifactId>
<version>0.89</version>
<artifactId>ballerina-core</artifactId>
<version>0.95.1</version>
</dependency>

</dependencies>
Expand All @@ -51,7 +51,7 @@

<!-- copy built-in ballerina sources to the jar -->
<resource>
<directory>${generated.ballerina.source.directory}</directory>
<directory>${project.build.directory}/../src/main/ballerina</directory>
<targetPath>META-INF/natives</targetPath>
</resource>
</resources>
Expand All @@ -73,13 +73,11 @@
<version>2.2.4</version>
<configuration>
<processors>
<processor>org.ballerinalang.natives.annotation.processor.BallerinaAnnotationProcessor</processor>
<processor>org.ballerinalang.codegen.BallerinaAnnotationProcessor</processor>
</processors>
<options>
<packageName>${native.constructs.provider.package}</packageName>
<className>${native.constructs.provider.class}</className>
<srcDir>${ballerina.source.directory}</srcDir>
<pkgRepositoryClass>${package.repository.provider.class}</pkgRepositoryClass>
<nativeEntityProviderPackage>org.wso2.sample.generated.providers</nativeEntityProviderPackage>
<nativeEntityProviderClass>StandardNativeElementProvider</nativeEntityProviderClass>
</options>
</configuration>
<executions>
Expand All @@ -92,21 +90,7 @@
</execution>
</executions>
</plugin>


</plugins>
</build>

<properties>
<!-- Path to the ballerina source directory -->
<ballerina.source.directory>${project.build.directory}/../src/main/ballerina</ballerina.source.directory>
<package.repository.provider.class>org.wso2.ballerina.math.repository.BallerinaBuiltinPackageRepository
</package.repository.provider.class>

<native.constructs.provider.package>org.wso2.ballerina.math</native.constructs.provider.package>
<native.constructs.provider.class>MathNativeConstructsProvider</native.constructs.provider.class>

<!-- Path to the generated natives ballerina files temp directory -->
<generated.ballerina.source.directory>${project.build.directory}/../src/main/ballerina</generated.ballerina.source.directory>
</properties>
</project>
7 changes: 0 additions & 7 deletions src/main/ballerina/wso2/ballerina/math/natives.bal

This file was deleted.

3 changes: 3 additions & 0 deletions src/main/ballerina/wso2/sample/math/natives.bal
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
package wso2.sample.math;

public native function nextInt (int bound) (int);
69 changes: 0 additions & 69 deletions src/main/java/org/wso2/ballerina/math/Exp.java

This file was deleted.

71 changes: 0 additions & 71 deletions src/main/java/org/wso2/ballerina/math/Pow.java

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -15,28 +15,32 @@
* specific language governing permissions and limitations
* under the License.
*/
package org.wso2.ballerina.math;
package org.wso2.sample.math;

import org.ballerinalang.bre.Context;
import org.ballerinalang.model.types.TypeEnum;
import org.ballerinalang.model.values.BFloat;
import org.ballerinalang.model.types.TypeKind;
import org.ballerinalang.model.values.BInteger;
import org.ballerinalang.model.values.BValue;
import org.ballerinalang.natives.AbstractNativeFunction;
import org.ballerinalang.natives.annotations.Argument;
import org.ballerinalang.natives.annotations.BallerinaFunction;
import org.ballerinalang.natives.annotations.ReturnType;

/**
* Native function wso2.ballerina.math:pow.
* Native function wso2.ballerina.math:nextInt.
*/
@BallerinaFunction(
packageName = "wso2.ballerina.math",
functionName = "random",
returnType = {@ReturnType(type = TypeEnum.FLOAT)},
packageName = "wso2.sample.math",
functionName = "nextInt",
args = {@Argument(name = "a", type = TypeKind.INT)},
returnType = {@ReturnType(type = TypeKind.INT)},
isPublic = true
)
public class Random extends AbstractNativeFunction {
public class NextInt extends AbstractNativeFunction {

public BValue[] execute(Context ctx) {
return getBValues(new BFloat(Math.random()));
int bound = (int) getIntArgument(ctx, 0);
int res = new java.util.Random().nextInt(bound);
return getBValues(new BInteger(res));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
/*
* Copyright (c) 2017, WSO2 Inc. (http://www.wso2.org) All Rights Reserved.
*
* WSO2 Inc. licenses this file to you under the Apache License,
* Version 2.0 (the "License"); you may not use this file except
* in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
package org.wso2.sample.math;

import org.ballerinalang.annotation.JavaSPIService;
import org.ballerinalang.repository.PackageRepository;
import org.ballerinalang.repository.fs.ClasspathPackageRepository;
import org.ballerinalang.spi.ExtensionPackageRepositoryProvider;
@JavaSPIService("org.ballerinalang.spi.ExtensionPackageRepositoryProvider")
public class StandardExtensionPackageRepositoryProvider implements ExtensionPackageRepositoryProvider {
private static final String JAR_SYSTEM_LIB_LOCATION = "/META-INF/natives/";
@Override
public PackageRepository loadRepository() {
return new ClasspathPackageRepository(this.getClass(), JAR_SYSTEM_LIB_LOCATION);
}
}

0 comments on commit b5b3f68

Please sign in to comment.