Skip to content

Commit

Permalink
ctx.load_script -> ctx.load
Browse files Browse the repository at this point in the history
  • Loading branch information
mtasic85 committed Nov 20, 2024
1 parent bb7aed7 commit 7e3f55f
Show file tree
Hide file tree
Showing 9 changed files with 153 additions and 139 deletions.
21 changes: 18 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,15 +1,30 @@
# quickjs-cffi

Python QuickJS CFFI
<!--
[![Build][build-image]]()
[![Status][status-image]][pypi-project-url]
[![Stable Version][stable-ver-image]][pypi-project-url]
[![Coverage][coverage-image]]()
[![Python][python-ver-image]][pypi-project-url]
[![License][mit-image]][mit-url]
-->
[![PyPI](https://img.shields.io/pypi/v/quickjs-cffi)](https://pypi.org/project/quickjs-cffi/)
[![Supported Versions](https://img.shields.io/pypi/pyversions/quickjs-cffi)](https://pypi.org/project/quickjs-cffi)
[![PyPI Downloads](https://img.shields.io/pypi/dm/quickjs-cffi)](https://pypistats.org/packages/quickjs-cffi)
[![Github Downloads](https://img.shields.io/github/downloads/tangledgroup/quickjs-cffi/total.svg?label=Github%20Downloads)]()
[![License: MIT](https://img.shields.io/badge/license-MIT-blue.svg)](https://opensource.org/licenses/MIT)

**Python** binding for [QuickJS Javascript Engine](https://bellard.org/quickjs/) using **cffi**. Supports **x86_64** and **aarch64** platforms.

NOTE: Currently supported operating system is Linux (`manylinux_2_28` and `musllinux_1_2`)

## Build

```bash
python -m venv venv
source venv/bin/activate
pip install poetry
poetry install
pip install tqdm
poetry install --all-extras
```

## Demos
Expand Down
2 changes: 1 addition & 1 deletion examples/demo_4.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ def demo4():
ctx: JSContext = rt.new_context()

script_url = 'https://raw.githubusercontent.com/lodash/lodash/refs/heads/main/dist/lodash.min.js'
ctx.load_script(script_url)
ctx.load(script_url)

lodash = ctx['_']
print(lodash, type(lodash))
Expand Down
6 changes: 2 additions & 4 deletions examples/demo_handlebars.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,7 @@ def demo_online():
rt = JSRuntime()
ctx: JSContext = rt.new_context()

script_url = 'https://cdnjs.cloudflare.com/ajax/libs/handlebars.js/4.7.8/handlebars.min.js'
ctx.load_script(script_url)
ctx.load('https://cdnjs.cloudflare.com/ajax/libs/handlebars.js/4.7.8/handlebars.min.js')

Handlebars = ctx['Handlebars']

Expand Down Expand Up @@ -38,8 +37,7 @@ def demo_offline():
rt = JSRuntime()
ctx: JSContext = rt.new_context()

script_url = 'node_modules/handlebars/dist/handlebars.min.js'
ctx.load_script(script_url)
ctx.load('node_modules/handlebars/dist/handlebars.min.js')

Handlebars = ctx['Handlebars']

Expand Down
4 changes: 2 additions & 2 deletions examples/demo_lodash.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ def demo_online():
rt = JSRuntime()
ctx: JSContext = rt.new_context()

ctx.load_script('https://raw.githubusercontent.com/lodash/lodash/refs/heads/main/dist/lodash.min.js')
ctx.load('https://raw.githubusercontent.com/lodash/lodash/refs/heads/main/dist/lodash.min.js')

lodash = ctx['_']
print(lodash)
Expand All @@ -19,7 +19,7 @@ def demo_offline():
rt = JSRuntime()
ctx: JSContext = rt.new_context()

ctx.load_script('node_modules/lodash/lodash.min.js')
ctx.load('node_modules/lodash/lodash.min.js')

lodash = ctx['_']
print(lodash)
Expand Down
6 changes: 3 additions & 3 deletions examples/demo_yjs.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ def demo0():
rt = JSRuntime()
ctx: JSContext = rt.new_context()

ctx.load_script('examples/yjs.js')
ctx.load('examples/yjs.js')

Y = ctx['Y']
# print(Y)
Expand All @@ -28,7 +28,7 @@ def demo1():
rt = JSRuntime()
ctx: JSContext = rt.new_context()

ctx.load_script('examples/yjs.js')
ctx.load('examples/yjs.js')

Y = ctx['Y']
# print(Y)
Expand All @@ -50,7 +50,7 @@ def demo2():
rt = JSRuntime()
ctx: JSContext = rt.new_context()

ctx.load_script('examples/yjs.js')
ctx.load('examples/yjs.js')

Y = ctx['Y']
# print(Y)
Expand Down
231 changes: 116 additions & 115 deletions poetry.lock

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
[tool.poetry]
name = "quickjs-cffi"
version = "0.1.0"
description = "Python binding for quickjs using cffi"
description = "Python binding for QuickJS using CFFI"
homepage = "https://github.com/tangledgroup/quickjs-cffi"
repository = "https://github.com/tangledgroup/quickjs-cffi"
authors = ["Marko Tasic <[email protected]>", "Tangled Group, Inc <[email protected]>"]
Expand Down
4 changes: 2 additions & 2 deletions quickjs/quickjs.py
Original file line number Diff line number Diff line change
Expand Up @@ -605,11 +605,11 @@ def eval(self, buf: str, filename: str='<inupt>', eval_flags: JSEval | int=JSEva
return val


def load_script(self, path_or_url: str) -> Any:
def load(self, path_or_url: str, eval_flags: JSEval | int=JSEval.TYPE_GLOBAL) -> Any:
path: str
data: str
path, data = read_script(path_or_url)
val: Any = self.eval(data, path)
val: Any = self.eval(data, path, eval_flags)
return val


Expand Down
16 changes: 8 additions & 8 deletions scripts/build.py
Original file line number Diff line number Diff line change
Expand Up @@ -234,16 +234,16 @@ def build_quickjs_repo(*args, **kwargs):
_source = '\n'.join(_quickjs_source)
_inline_static_source = '\n'.join(_inline_static_source)

print('='* 80)
print(_inline_static_source)
print('='* 80)
# print('='* 80)
# print(_inline_static_source)
# print('='* 80)

# function declarations for inlined definitions
func_declarations: list[str] | str = get_func_declarations(_inline_static_source)
func_declarations = '\n'.join(func_declarations)
print('-'* 80)
print(func_declarations)
print('-'* 80)
# print('-'* 80)
# print(func_declarations)
# print('-'* 80)
_source += '\n\n' + func_declarations

# extra source declarations
Expand All @@ -270,8 +270,8 @@ def build_quickjs_repo(*args, **kwargs):
'''

# print code
for i, line in enumerate(_source.splitlines()):
print(i + 1, ':', line)
# for i, line in enumerate(_source.splitlines()):
# print(i + 1, ':', line)

# build
ffibuilder.cdef(_source)
Expand Down

0 comments on commit 7e3f55f

Please sign in to comment.