Giter VIP home page Giter VIP logo

l8framework's Introduction

L8Framework

A framework wrapping V8 into Objective-C.

Nearly compatible with the JavaScriptCore Objective-C API by Apple Inc.

Building L8Framework

To build L8Framework, only a couple of simple steps are required:

First, clone the repository and load the submodules:

$ git clone https://github.com/joskuijpers/L8Framework.git
$ cd L8Framework
$ git submodule init
$ git submodule update

Then run the v8_build script, to download v8 dependencies, configure and patch the build system, and to build v8:

$ ./v8_build.sh

Then either open the Xcode project and build, or use:

$ xcodebuild

Examples

A simple 2+2 program:

L8Runtime *runtime = [[L8Runtime alloc] init];
L8Value *value = [runtime evaluateScript:@"2+2"];

// The -description of an L8Value is the same as value.toString()
// in JavaScript.
NSLog(@"2+2 = %@",value);

// You can also convert to an NSNumber and take the integer value.
NSLog(@"2+2 = %ld",[[value toNumber] integerValue]);

A hello world program using an Objective-C console class. V8 does not supply a console natively.

// The protocol, inheriting from L8Export, tells the L8Framework
// what properties and methods to export to JavaScript.
@protocol MyConsole <L8Export>
- (void)log:(NSString *)message;
@end

@interface MyConsole : NSObject <MyConsole>
@end

@implementation MyConsole

// The L8Framework will convert any input type to the requested class:
// here that is an NSString. So even if console.log(5) is ran, you will
// receive @"5".
// To request the 'raw' arguments, all of them, you can run
// [L8Runtime currentArguments]. This works just like the this.arguments
// in JavaScript.
- (void)log:(NSString *)message
{
	NSLog(@"[JS] %@",message);
}

@end

L8Runtime *runtime = [[L8Runtime alloc] init];

// Because of the way V8 works with scoped memory management,
// and because of how Cocoa works with events, we need to enter
// the runtime context before doing some actual code.
// Any methods in classes called by the runtime are within context.
[runtime executeBlockInRuntime:^(L8Runtime *runtime) {
	runtime[@"console"] = [[MyConsole alloc] init];
}];

[runtime evaluateScript:@"console.log('Hello World!')"];

HINT: If you are not sure about whether you need -[executeBlockInRuntime:], then do not use it. When it runs and it is not in context, V8 will tell you you are not in a context scope and abort.

License

This software is released under the 2 clause BSD license. See LICENSE.

Copyright (c) 2014 Jos Kuijpers. All rights reserved.

Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions
are met:
1. Redistributions of source code must retain the above copyright
   notice, this list of conditions and the following disclaimer.
2. Redistributions in binary form must reproduce the above copyright
   notice, this list of conditions and the following disclaimer in the
   documentation and/or other materials provided with the distribution.

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE 
USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

l8framework's People

Contributors

joskuijpers avatar

Stargazers

 avatar Mark Sinkovics avatar 新房 森人 avatar Joey Gouly avatar Freya Alminde avatar

Watchers

mayulu avatar James Cloos avatar  avatar

l8framework's Issues

Add L8 runtime configuration and information access

The L8 runtime might need to be configured at runtime. For example, enabling JavaScript flags.
L8 should also give information about itself and the V8 engine. The most important here is the engine version. (V8::GetVersion()).

  • Add L8 static class.
  • Add v8 version getter.+(NSString *)V8Version;
  • Add L8 version getter. +(NSString *)L8Version;
  • Allow configuration of Strict Mode: +(void)forceStrictMode

Finish first API version

At least the following needs to be added/implemented:

  • -[L8Runtime currentArguments]
  • -[L8Runtime currentThis]
  • -[L8Runtime currentCallee]
  • -[L8Runtime currentRuntime] ; Re-implement
  • -[L8Value toFunction] ; Or remove
  • -[L8Value isEqualWithTypeCoercionToObject:]
  • -[L8Value isInstanceOf:]
  • L8ManagedValue
  • -[L8Runtime addManagedReference:withOwner:]
  • -[L8Runtime removeManagedReference:withOwner:]
  • In ObjCConstructor, do not lookup init method. Instead, make constructor available if one (1) init method is in the protocol and assign its selector and type.
  • Add struct support for arguments and return types
  • Transform well known argument types (NSString, NSArray, L8Value,...) to the actual classes to cause objc method to receive the arguments with expected class.
  • Add caching of classes
  • Fix the mysterious -init handling code
  • Verify block return values work
  • Implement property queries
  • Verify arguments of Accessor Setter using valueType
  • Add range checks in argument creation
  • Implement Class return arguments
  • Find out if the subscription transform makes sense at all
  • Verify or implement object inheritance
  • Implement -[L8Value defineProperty...]
  • Verify exception transfer from ObjC to JS works
  • Verify exception transfer from JS to ObjC works
  • Fast Enumeration in L8StackTrace

Make multiple contexts possible

The API of L8Framework could be changed to match even more like JavaScriptCore:

L8Runtime now wraps both v8::Context and v8::Isolate. It could be made into L8Context, wrapping v8::Context in a specific isolate, namely L8VirtualMachine, which wraps v8::Isolate.

Managed values only use an isolate so they go into L8VirtualMachine. L8Value is bound to a context, so -[valueWithXX:] changes into -[valueWithXX:inContext:], just like JSC.

If there is interest in this, it can be build in a new branch. It is however not backward compatible. (it can be, but that makes it very messy).

Write more tests

There is not enough code coverage from units tests.

  • Calling an objc method from js, that calls into an objc method via js. And then the validaty of [L8Runtime current*] after the second call.
  • L8ArrayBuffer tests.
  • Symbol tests.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.